You can query web APIs easily with curl. If the API endpoint accepts GET requests, then this is as simple as:
(The quotes are important here so that your shell doesn’t interpret &
as a request to background curl!)
For APIs that use GET/POST/PUT/etc. request types, combine the --data
and --request
parameters:
The --data
parameter can also be specified multiple times (mostly for readability), in which case you’d normally want to have a single parameter/value pair for each instance.
By default, curl sends data with the Content-Type: application/x-www-form-urlencoded
. If you need to change the content type (for example, you frequently need to send Content-Type: application/json
with JSON data) or need to specify additional headers (frequently for authentication), then you can use the --header
parameter.
Like --data
, the --header
parameter can be specified multiple times for multiple headers, and will smartly override curl’s defaults.
Because responses are often served up in a compact fashion, they’re often a bit hard to read. The jq command’s default filter (.
) just pretty-prints (and colorizes!) JSON, which can make interpreting the API’s response much easier.