Skip to content

Instantly share code, notes, and snippets.

@bagder
Last active March 23, 2022 03:09
Show Gist options
  • Save bagder/f3a08811112acbd7a3f0c9c8265b99c4 to your computer and use it in GitHub Desktop.
Save bagder/f3a08811112acbd7a3f0c9c8265b99c4 to your computer and use it in GitHub Desktop.
HTTP header access in curl

HTTP response header access in curl

This support depends on the new headers API work coming in PR 8593. Hopefully landing in 7.83.0.

This output is done with -w and new magic added to its abilties.

Output individual header contents

Say hello to %header{name} where name is the case insensitive name of the header withon colon.

$ curl -w '%header{date}\n' -o savefile curl.se
Thu, 17 Mar 2022 16:59:24 GMT

Output all response headers as JSON

Say hello to %{header_json}

$ curl -sw '%{header_json}\n' -o savefile curl.se | jq
{
  "Date": [
    "Tue, 09 Nov 2010 14:49:00 GMT"
  ],
  "Server": [
    "test-server/fake"
  ],
  "Last-Modified": [
    "Tue, 13 Jun 2000 12:10:00 GMT"
  ],
  "ETag": [
    "\"21025-dc7-39462498\""
  ],
  "Accept-Ranges": [
    "bytes"
  ],
  "Set-Cookie": [
    "firstcookie=want; path=/",
    "2cookie=want; path=/",
    "cookie3=want; path=/"
  ],
  "Content-Length": [
    "6"
  ],
  "Connection": [
    "close"
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment