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 -w %{json} dumps every transfer detail as one JSON object

Curl
~/tips/tip-curl-write-out-json

curl -s -o /dev/null -w '%{json}' https://example.com/ | jq .

Get all write-out values at once and pick fields with jq.

The names of the write-out variables are hard to remember. %{json} prints all of them in one go, so I’d look at the whole list first and pick what you need.

Send the body away and pipe the JSON into jq:

curl -s -o /dev/null -w '%{json}' https://example.com/ | jq .

You get keys like http_code, http_version, remote_ip, time_connect, time_total, num_redirects, exitcode and errormsg, plus many more. To keep just a few fields:

curl -s -o /dev/null -w '%{json}' https://example.com/ \
  | jq '{code: .http_code, ip: .remote_ip, total: .time_total}'

That makes it easy to log checks as JSON lines and read them later with jq. A sibling, %{header_json}, does the same for the response headers.

Things to keep in mind: the set of keys depends on your curl version, so a field from a newer release may simply be missing on an older box. Values like timings are numbers in seconds, not strings with a unit. And the output follows the body, so if you forget -o /dev/null the page and the JSON end up mixed on standard output and jq fails. The variable needs curl 7.70 or newer.

Further reading: curl man page, everything curl: write out

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