[英]How to get http status code and content separately using curl in linux
[英]Get http code when uploading file using curl and printing curl output
我正在編寫一個 bash 腳本,我想在其中使用 curl 上傳文件。 我想在日志文件中打印來自 stdout 和 stderr 的 output(查看上傳是否完成)。 到目前為止,這是有效的:
curl -T home/user/mydir/filename <my url>/.../filename >> home/user/mydir/script.log 2>&1
現在,我還想獲取返回的 http 代碼,以便可以在腳本的其他位置使用它。 以下也有效:
CODE=$(curl -w '%{http_code}' -T home/user/mydir/filename <my url>/.../filename)
如何結合這兩個命令? 我想在日志文件中打印標准輸出和標准錯誤(准確地說是附加),並將返回的 http 代碼存儲在一個變量中以供以后使用。
謝謝你。
你能做這樣的事情嗎(第一行是返回碼)(來自詳細選項)。 -o 可以是 - (stdout) 或您想要的任何其他文件?
# curl -v -o /dev/null http://www.google.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 142.250.76.100...
* TCP_NODELAY set
* Connected to www.google.com (142.250.76.100) port 80 (#0)
> GET / HTTP/1.1
> Host: www.google.com
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Wed, 10 Mar 2021 02:37:46 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< Content-Type: text/html; charset=ISO-8859-1
< P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
< Server: gws
< X-XSS-Protection: 0
< X-Frame-Options: SAMEORIGIN
< Set-Cookie: 1P_JAR=2021-03-10-02; expires=Fri, 09-Apr-2021 02:37:46 GMT; path=/; domain=.google.com; Secure
< Set-Cookie: NID=211=l3bDHkrWaIiQN03V4QI2bXrwHu0ZTMxoQgB1DDhIE2EfomQO6zrAPbu-5h6L6Ru60kQh0vtAog3iykbLvvtv28r8aVYiyMapXQWTexMArSIqRVQrcwDL4PnaoivdpE1_aL0rC6gohDpxFT-yQmk3jkE7EKtdSL_Dh7Z7nsnoqBM; expires=Thu, 09-Sep-2021 02:37:46 GMT; path=/; domain=.google.com; HttpOnly
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
<
{ [686 bytes data]
100 13716 0 13716 0 0 37889 0 --:--:-- --:--:-- --:--:-- 37889
* Connection #0 to host www.google.com left intact
* Closing connection 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.