![](/img/trans.png)
[英]How to capture actual response code and response body with HTTPie in bash script?
[英]How to pipe header information of a httpie request in bash?
我正在使用httpie針對此文件位置發出HEAD請求:
$ 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)
我只對Location
標頭感興趣,因為我想將其值存儲在文件中以查看目標是否已更新。
我試過了:
http HEAD https://dbeaver.io/files/dbeaver-ce_latest_amd64.deb \
| grep Location \
| sed "s/Location: //"
但這會產生空的響應。
我假設輸出將輸出到stderr
而不是stdout
,盡管我真的不想為此組合stdout
和stderr
。
我寧願直接使用http
命令尋找解決方案。
您缺少--header
選項:
http HEAD https://dbeaver.io/files/dbeaver-ce_latest_amd64.deb \
--headers \
| grep Location \
| sed "s/Location: //"
在撰寫本文時將:
https://dbeaver.io/files/6.2.0/dbeaver-ce_6.2.0_amd64.deb
此外,您認為httpie將重定向到stderr
假設也是錯誤的。 而是歸結為--print
選項的自動更改默認行為。 如果httpie
已通過管道傳輸,那就改變了!
--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.
--header
/ -h
選項僅僅是--print=h
的快捷方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.