k0pernikus
k0pernikus

Reputation: 66787

How to pipe header information of a httpie request in bash?

I am making a HEAD request against this file location using httpie:

$ http HEAD https://dbeaver.io/files/dbeaver-ce_latest_amd64.deb 
HTTP/1.1 302 Moved Temporarily
Connection: keep-alive
Content-Length: 169
Content-Type: text/html
Date: Mon, 09 Sep 2019 14:55:56 GMT
Location: https://dbeaver.io/files/6.2.0/dbeaver-ce_6.2.0_amd64.deb
Server: nginx/1.4.6 (Ubuntu)

I am only interested in the Location header as I want to store its value in a file to see if it the target was updated.

I tried:

http HEAD https://dbeaver.io/files/dbeaver-ce_latest_amd64.deb \
    | grep Location \
    | sed "s/Location: //"

yet this yields in an empty response.

I assume the output goes to stderr instead of stdout, though I don't really want to combine stdout and stderr for this.

I am rather looking for a solution directly with the http command.

Upvotes: 1

Views: 1143

Answers (1)

k0pernikus
k0pernikus

Reputation: 66787

You are missing the --header option:

http HEAD https://dbeaver.io/files/dbeaver-ce_latest_amd64.deb \
    --headers \
    | grep Location \
    | sed "s/Location: //"

will as of this writing print:

https://dbeaver.io/files/6.2.0/dbeaver-ce_6.2.0_amd64.deb

Furthermore, your assumption that httpie would redirect to stderr is also wrong. Instead, it boils down to the automatically changing default behavior of the --print option. And it changes on the fact if the httpie was piped!

--print WHAT, -p WHAT
  String specifying what the output should contain:

      'H' request headers
      'B' request body
      'h' response headers
      'b' response body

  The default behaviour is 'hb' (i.e., the response headers and body
  is printed), if standard output is not redirected. If the output is piped
  to another program or to a file, then only the response body is printed
  by default.

The --header/ -h option is merely a shortcut for --print=h.

Upvotes: 3

Related Questions