brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: curl --json posts JSON with the right headers in one flag

Curl
~/tips/tip-curl-json-flag-shorthand

curl --json '{"name":"test","qty":2}' https://api.example.com/items

Sends a JSON body together with the matching content-type and accept headers.

Every JSON request used to need a `-H ‘Content-Type: application/json’` line next to the data.

curl --json '{"name":"test","qty":2}' https://api.example.com/items
curl --json @payload.json https://api.example.com/items

--json (7.82.0 and newer) means --data-binary plus the headers Content-Type: application/json and Accept: application/json. A leading @ reads the body from a file, which is sent as it is, newlines included. Use that form for large payloads so the shell does not need to quote them. curl does not validate the JSON, so a typo reaches the server unchanged. curl -V shows your version if the flag is rejected. On older builds the long form is -H twice plus --data-binary.

Further reading: curl manual

More Curl tips curl --trace-ascii – https://example.com/ Retry-After: 120 curl -fsS --fail-early \ url = "https://example.com/data/latest.json" curl -4 -s -o /dev/null -w '%{remote_ip} %{http_code}\n' https://example.com/ All Curl tips (71) All topics

Link