Linux

捲曲從命令行僅返回 http 狀態程式碼

  • June 3, 2021

我有下面的 curl 從文件中讀取數據並將其發佈到伺服器,一切正常。我也成功地得到了回复。

curl -v ‘url’ -H ‘Accept-Encoding: gzip, deflate, br’ -H ‘Content-Type: application/json’ -H ‘Accept: application/json’ -H ‘Connection: keep-alive’ -H ‘DNT: 1’ -H ‘Origin: url’ –data-binary “@/Users/david/Downloads/temp.txt” –compressed

現在我只是想從上面的 curl 請求中獲取狀態碼,而不是完整的響應。我嘗試像下面那樣做,但它不起作用。

curl -v ‘url’ -H ‘Accept-Encoding: gzip, deflate, br’ -H ‘Content-Type: application/json’ -H ‘Accept: application/json’ -H ‘Connection: keep-alive’ -H ‘DNT: 1’ -H ‘Origin: url’ –data-binary “@/Users/david/Downloads/temp.txt” –compressed | 頭-n 1 | 剪切-d$’’-f2

我上面的命令有什麼問題嗎?

刪除-v和添加--silent,丟棄標準輸出並使用(簡而言之)--output /dev/null 列印http狀態:--write-out '%{http_code}'-s -o /dev/null -w '%{http_code}'

curl 'url' \
 -H 'Accept-Encoding: gzip, deflate, br' \
 -H 'Content-Type: application/json' \
 -H 'Accept: application/json' \
 -H 'Connection: keep-alive' \
 -H 'DNT: 1' \
 -H 'Origin: url' \
 --data-binary "@/Users/david/Downloads/temp.txt" \
 --compressed \
 --silent \
 --output /dev/null \
 --write-out '%{http_code}'

引用自:https://unix.stackexchange.com/questions/652536