Using curl and jq with Web APIs

curl is the standard command-line HTTP client — it speaks every method, header, and auth scheme, and pipes naturally into jq, the standard command-line JSON processor. Together they form a complete API interaction toolkit that requires no code beyond a shell prompt. 1 2

GET requests

curl "https://web.site/?parameter1=value1&parameter2=value2"

The quotes are critical — without them, the shell interprets & as a background operator.

POST with form data

curl --request POST \
     --data "parameter1=value1&parameter2=value2" \
     "https://web.site/"

Or split parameters for readability:

curl --request POST \
     --data "parameter1=value1" \
     --data "parameter2=value2" \
     "https://web.site/"

By default, curl sends Content-Type: application/x-www-form-urlencoded.

Custom headers

Use --header (repeatable) to override content type, add auth tokens, or set any other header:

curl --request POST \
     --header "Content-Type: application/json" \
     --header "User-Token: XXXXXX" \
     --data '{"key": "value"}' \
     "https://web.site/"

Pretty-printing responses with jq

The default jq filter (.) pretty-prints and colorizes JSON:

curl --silent "https://api.example.com/data" | jq .

Extract specific fields:

curl --silent "https://api.example.com/users" | jq '.[].name'

Combine with jq’s built-in functions for filtering, mapping, and restructuring — jq is a full functional language for JSON, not just a formatter.

Sources

Related: bash-scripting, ftp, invoke-webrequest-download-cradles

Footnotes

  1. curl man page

  2. jq 1.8 Manual