我想發送一個帶有請求正文的 HEAD 請求。
所以我嘗試了以下命令。但我有一些錯誤。
$ curl -X HEAD http://localhost:8080 -d "test"
Warning: Setting custom HTTP method to HEAD with -X/--request may not work the
Warning: way you want. Consider using -I/--head instead.
curl: (18) transfer closed with 11 bytes remaining to read
或者我試過這個:
$ curl -I http://localhost:8080 -d "test"
Warning: You can only select one HTTP request method! You asked for both POST
Warning: (-d, --data) and HEAD (-I, --head).
我認為 RFC 不禁止發送帶有請求正文的 HEAD 請求。
我怎樣才能發送?
uj5u.com熱心網友回復:
默認情況下,使用-d/ --data,使用方法“ POST”。
With -I/ --headyou suggest to use " HEAD" 方法。
您的服務如何接受哪種方法(POST 或 HEAD)?
我使用“ https://httpbin.org”網站進行測驗。
使用 cURL,您可以像這樣使用 POST:
$ curl --silent --include https://httpbin.org/post -d "data=spam_and_eggs"
HTTP/2 200
date: Thu, 30 Sep 2021 18:57:02 GMT
content-type: application/json
content-length: 438
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true
{
"args": {},
"data": "",
"files": {},
"form": {
"data": "spam_and_eggs"
},
"headers": {
"Accept": "*/*",
"Content-Length": "18",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.71.1",
"X-Amzn-Trace-Id": "Root=1-6156087e-6b04f4645dce993909a95b24"
},
"json": null,
"origin": "86.245.210.158",
"url": "https://httpbin.org/post"
}
或“ HEAD”方法:
$ curl --silent -X HEAD --include https://httpbin.org/headers -d "data=spam_and_eggs"
HTTP/2 200
date: Thu, 30 Sep 2021 18:58:30 GMT
content-type: application/json
content-length: 260
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true
我用strace(使用 HTTP 協議)檢查了HEAD帶有資料的請求被傳遞到服務器:
sendto(5, "HEAD /headers HTTP/1.1\r\nHost: httpbin.org\r\nUser-Agent: curl/7.71.1\r\nAccept: */*\r\nContent-Length: 18\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\ndata=spam_and_eggs", 170, MSG_NOSIGNAL, NULL, 0) = 170
當然,如果沒有“ --silent”選項,就會出現警告資訊:
Warning: Setting custom HTTP method to HEAD with -X/--request may not work the
Warning: way you want. Consider using -I/--head instead.
我的研究基于這篇非常古老的帖子:https : //serverfault.com/questions/140149/difference-between-curl-i-and-curl-x-head
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321200.html
上一篇:在GCP上安裝Citus失敗
下一篇:正則運算式在術語之前選擇文本
