The Computer Oracle

Getting curl to output HTTP status code?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over

--

Chapters
00:00 Question
00:35 Accepted answer (Score 886)
00:57 Answer 2 (Score 1375)
01:51 Answer 3 (Score 464)
02:09 Answer 4 (Score 284)
02:32 Thank you

--

Full question
https://superuser.com/questions/272265/g...

Answer 1 links:
[available variables for the ]: https://curl.se/docs/manpage.html#-w

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#http #curl #status

#avk47



ANSWER 1

Score 1497


A more specific way to print out just the HTTP status code is something along the lines of:

curl -s -o /dev/null -w "%{http_code}" http://www.example.org/

A lot easier to work with in scripts, as it doesn't require any parsing :-)

The parameter -I might be added to improve response load performance. This will change the call to a HEAD call which will fetch response overhead only, without the body.

Note: %{http_code} returns on first line of HTTP payload (available variables for the -w option on the curl documentation page)

i.e.:

curl -s -o /dev/null -I -w "%{http_code}" http://www.example.org/



ACCEPTED ANSWER

Score 970


This should work for you if the web server is able to respond to HEAD requests (this will not perform a GET request):

curl -I http://www.example.org

As an addition, to let cURL follow redirects (3xx statuses) add -L.




ANSWER 3

Score 552


You can print the status code, in addition to all the headers by doing the following:

curl -i http://example.org

The good thing about -i is that it works with -X POST as well.




ANSWER 4

Score 295


If you want to see the header as well as the result you can use the verbose option:

curl -v http://www.example.org
curl --verbose http://www.example.org

The status will appear in the header. E.g.

< Date: Tue, 04 Nov 2014 19:12:59 GMT
< Content-Type: application/json; charset=utf-8
< Status: 422 Unprocessable Entity