14

I make a cURL request using:

PATCH=$(curl -i -F file=@$FILE -F path="${STORAGE_PATH}" -F name="${NAME}" -F description="${DESC}" "${SERVER}/api/patches")

If the response goes through and the data is property formatted, then PATCH would be a JSON of the response.

I also want to get the HTTP response code (like 200, 422) at the same time, so I can verify that the process went through.

how do I do that? I just want to get a number (like 200).

Kousha
  • 32,871
  • 51
  • 172
  • 296
  • Maybe this can help http://www.commandlinefu.com/commands/view/13818/get-http-status-code-with-curl-and-print-response-on-new-line – dafyk Mar 24 '15 at 21:58
  • And here is example using netcat instead of curl http://stackoverflow.com/a/2220768/1368752 – dafyk Mar 24 '15 at 22:06
  • This is only giving me the status code, I want to get the actual server response data as well (Get the JSON data AND the response code) – Kousha Mar 24 '15 at 22:57

2 Answers2

16

I used this post to solve my problem and thought I'd share my results. My goal was to create a script to make sure my accesstoken endpoint was working. So I had to make a POST call and extract the response code, here was my end result:

status=$(curl -w "%{http_code}\\n" -H "Accept:application/json" -H "Content-Type:application/x-www-form-urlencoded" --data "client_id=blah&client_secret=blah&grant_type=password&user_name=user&password=pass" https://api.company.com/v1/accessToken -s -o /dev/null)

Explanation

  • status=$({curlRequest}) will store the output into a bash variable
  • -w will extract the status code from the response
  • -H configures my HTTP header request
  • --data sets the payload data that I want to POST (this flag also automatically sets the request to POST
  • -s will silence progress meter of the request
  • -o this will extract the response body and put it into a file. By setting the value to /dev/null, the output is discarded

The key values here are -w and -o if you only want a response code. Remove the -o flag to keep the response body.

Cyrois
  • 439
  • 4
  • 9
  • 2
    Thank you, it works, but when I see `-a -b -c -b -z -y -c` I think why do you guys do not use full forms like `--silent --header --write-out` – Vitaly Zdanevich Apr 05 '19 at 11:17
  • 1
    I get confused when I see that too, particularly when I was just starting out. As you start to memorize the command, you want to type the least amount of characters as possible. It is purely for development speed. – Cyrois May 07 '19 at 16:55
12

You can use:

PATCH=$(curl -L -w "%{http_code} %{url_effective}\\n" -X POST -d @filename.txt ${server}/api/patches --header "Content-Type:application/json")

It will give you both the response code and body.

Crackertastic
  • 4,958
  • 2
  • 30
  • 37
Saket Saurabh
  • 178
  • 3
  • 8
  • can you elaborate a bit more of how should i extract the status code out of the PATCH variable ? – danfromisrael Apr 17 '18 at 13:26
  • 2
    `STATUS=$(echo $PATCH | tail -n 1)` will store the status (last line) in the `$STATUS` and `BODY=$(echo $PATCH | sed '$d')` will store the body (everything but the last line) in `$BODY`. – gabrielf May 09 '18 at 11:59