Is it possible to use curl to call a REST service (POST method) and get:

  • The HTTP status code.
  • The response body.

Other information like headers, methods, etc. are not relevant for my use case and actually add to the confusion when testing.

For example, I'm doing:

$ curl -i -H 'Content-Type: application/json' -d @payload.json localhost:8080/apply
HTTP/1.1 100 

HTTP/1.1 400 
Content-Type: text/plain;charset=UTF-8
Content-Length: 42
Date: Fri, 22 Oct 2021 16:29:18 GMT
Connection: close

Invalid product: product does not exist

I know the service is returning 400 that I can see, and also the response error Invalid product: product does not exist that I can also see, so that's good.

However, is it possible to get rid of the rest of the display?

2 Answers 2


Instead of -i to display the response headers, you could use -w/--write-out with a format string containing the http_code variable:

curl --write-out '%{http_code}\n' ...

would print the response status (and a newline) after the body. Check man curl for other variables you might find useful.

  • 1
    Almost perfect! It's displaying the code after the body. Do you know how to display it before? If you don't know that's OK, your solution is already good. Oct 22, 2021 at 17:59
  • That doesn't seem possible: The man page says "Make curl display information on stdout after a completed transfer", and some of the variables are only available after the transfer is complete anyway. You could write a script that calls curl with --output to write the body to a temporary file and then cats that after the --write-out result, though.
    – DonHolgo
    Oct 22, 2021 at 21:18
  • Thank you. It's more than useful as it is. Oct 23, 2021 at 2:09

Building on @DonHolgo 's solution, one can add more info in the write-out like this:

curl --location 'https://foo.acme.com' \
--request POST \
--write-out '\nhttp_code=%{http_code}\nexitcode=%{exitcode}\n' --silent --show-error \
--header 'Content-Type: application/json' \
--data '{"foo": "bar","baz":"bim"}'

The output in this case is combined with the response from POST:


You can capture that in one variable and process 3 things: the POST response (JSON), the HTTP response code, and the exit code from cURL command.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .