Curl: How to Send Head Request with Request Body

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  -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  -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 ?

1 Answer

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 "" site for testing.

With cURL, yout could use, POST like this:

$ curl --silent --include  -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": ""
}

or "HEAD" method:

$ curl --silent -X HEAD --include  -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:

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest