Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have curl print just the response code?

Tags:

http

curl

I read https://superuser.com/questions/272265/getting-curl-to-output-http-status-code . It mentioned that

curl -i 

will print the HTTP response code. Is it possible to have curl print just the HTTP response code? Is there a generic way to get the HTTP status code for any type of request like GET/POST/etc?

I am using curl 7.54.0 on Mac OS High Sierra.

Thanks for reading.

like image 545
user674669 Avatar asked Nov 28 '18 18:11

user674669


People also ask

How do I print a curl response?

By default, curl doesn't print the response headers. It only prints the response body. To print the response headers, too, use the -i command line argument.

How do you get the curl response header?

We can use curl -v or curl -verbose to display the request headers and response headers in the cURL command. The > lines are request headers . The < lines are response headers .

What is curl in response?

'cURL' is a command-line tool that lets you transmit HTTP requests and receive responses from the command line or a shell script. It is available for Linux distributions, Mac OS X, and Windows. To use cURL to run your REST web API call, use the cURL command syntax to construct the command.


2 Answers

This worked for me:

$  curl -s -w "%{http_code}\n" http://google.com/ -o /dev/null
like image 124
user674669 Avatar answered Sep 30 '22 02:09

user674669


curl -s -I http://example.org | grep HTTP/ | awk {'print $2'}

output: 200

like image 30
Uladzimir Avatar answered Sep 30 '22 02:09

Uladzimir