我想发送一个带有请求正文的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请求。
如何发送?
默认为-d
/--data
,方法"POST
";使用。
对于-I
/--head
,建议使用"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.1rnHost: httpbin.orgrnUser-Agent: curl/7.71.1rnAccept: */*rnContent-Length: 18rnContent-Type: application/x-www-form-urlencodedrnrndata=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