Home > Blockchain >  curl: How to send HEAD request with request body
curl: How to send HEAD request with request body

Time:10-18

I'd like to send a HEAD request with a request body.

So I tried the below commands. But I got some errors.

$ 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

or I tried this one:

$ 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).

I think that RFC doesn't prohibit sending HEAD request with a request body.

How can I send ?

CodePudding user response:

By default, with -d/--data, method "POST" is used.

With -I/--head you sugest to use "HEAD" method.

How your service accept which method (POST or HEAD) ?

I use "https://httpbin.org" site for testing.

With cURL, yout could use, POST like this:

$ 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"
}

or "HEAD" method:

$ 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

I inspected with strace (with the HTTP protocol) the HEAD request with data are passed to the server:

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

Of course, without "--silent" option, the warning message appears:

Warning: Setting custom HTTP method to HEAD with -X/--request may not work the 
Warning: way you want. Consider using -I/--head instead.

My research are based on this very old post: https://serverfault.com/questions/140149/difference-between-curl-i-and-curl-x-head

  • Related