Send JSON with curl

POST a JSON body to an API from the terminal, and read the answer.

SNP 058 #B12 Filed curl cmd

shell
curl -X POST https://api.example.com/posts \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer TOKEN' \
  -d '{"title": "Hello", "draft": true}'

Newer curl (7.82+) has --json, which sets both the content type and the accept header:

shell
curl --json '{"title": "Hello"}' https://api.example.com/posts

Read the body from a file with -d @post.json, and add -i to see the response headers, or -s -o /dev/null -w '%{http_code}\n' to see only the status code.