Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In bash get the Curl return code like 200 [duplicate]

I just wanted to know what is the way to get the return code from the curl command. Likewise, if trigger the same curl from postman client we are able to get all error messages.

Currently, I am just able to get the return code as "0" in case of success but I wanted "200" and other return code with respect to the command.

curl -X POST http://localhost:4449/api/messages -H 'accept: application/json' -H 'authorization: Basic cmVsYXktnruDFjEyMw==' -H 'content-type: application/json' -d "{\"cloudDelivery\":\"$cloudDelivery\"}"
like image 987
Indrajeet Gour Avatar asked Dec 05 '25 23:12

Indrajeet Gour


1 Answers

Use the -I option to get the status code on the first line of the response:

$ curl -I www.google.com
 HTTP/1.1 200 OK
 Date: Wed, 06 Feb 2019 12:58:31 GMT
 ...

There is a whole exchange about this question here.

They propose a simple way to get only the code with the next command:

curl -s -o /dev/null -I -w "%{http_code}" http://www.example.org/
like image 137
Victor Calatramas Avatar answered Dec 09 '25 13:12

Victor Calatramas