How to Show HTTP Status Code in cURL
cURL is the swiss army knife tool for data transfer between a client and a server. When working in the command-line, you will most often use curl for making various types of network requests, such as HTTP/HTTPS request, file downloads, etc.
cURL provides features such as:
- High portability. It is compatible with almost every operating system and connected device.
- It is useful for testing endpoints, to check if they are working.
- Verbosity. providing details of exactly what has been sent/received, which is helpful for debugging.
- Exceptional error logging features
- Rate Limiting
It is therefore no surprise that may developers and sys admins use it for normal endpoint testing.
In this post, we will discuss how we can allow cURL to show the HTTP status code from a given request.
Method 1 - Making A HEAD Request
The simplest method to get the HTTP status code from a given resource is by making a HTTP HEAD request. Instead of making a GET request to fetch all the data from the set resource, the HEAD method returns the HTTP-headers which contains the status code.
You can learn more about HTTP Status codes in the resource below:
https://www.geekbits.io/http-https-status-codes-cheatsheet/
To make a HEAD request using cURL, use the -I
option. The command syntax is as shown:
curl -I <address>
Example:
curl -I https://www.geekbits.io
The request above should return the HTTP headers for the specified resource as:
HTTP/2 200
server: openresty
content-type: text/html; charset=utf-8
status: 200 OK
x-request-id: 35916faa2a3a417c82fea4e512b9ac14
etag: W/"2e6c0-DY3LN9o/Zfak907RHPGgcCpaQUo"
ghost-cache: HIT
cache-control: public, max-age=0
ghost-age: 2451
x-request-id: 0d4aa711b24cc19862c75f1ec90e0433
via: 1.1 varnish, 1.1 varnish
accept-ranges: bytes
date: Sun, 02 Oct 2022 06:34:46 GMT
age: 4179
x-served-by: cache-ams21038-AMS, cache-jnb7026-JNB
x-cache: HIT, HIT
x-cache-hits: 3, 3
x-timer: S1664692486.393590,VS0,VE0
vary: Accept-Encoding, Cookie
ghost-fastly: true
alt-svc: clear
content-length: 190144
In the output above, we can see the server responds with 200 OK
status code. If you wish to filter only for Status code, pipe the output to grep
as:
curl -I https://www.geekbits.io | grep status
Output:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 185k 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
status: 200 OK
We can see the output status code.
Method 2 - Using cURL Verbose Mode
As mentioned earlier, cURL is extremely verbose. This means you can see every operation that occurs when you request a given resource.
One information that cURL will ouput is the HTTP status code. To run cURL in verbose mode:
curl -v http://www.example.org
cURL will return an output as:
* Trying 93.184.216.34:80...
* Connected to www.example.org (93.184.216.34) port 80 (#0)
> GET / HTTP/1.1
> Host: www.example.org
> User-Agent: curl/7.82.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Age: 312699
< Cache-Control: max-age=604800
< Content-Type: text/html; charset=UTF-8
< Date: Sun, 02 Oct 2022 06:40:09 GMT
< Etag: "3147526947+ident"
< Expires: Sun, 09 Oct 2022 06:40:09 GMT
< Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
< Server: ECS (nyb/1D20)
< Vary: Accept-Encoding
< X-Cache: HIT
< Content-Length: 1256
<--------------------------------truncated----------------------------
In this case, we can see the HTTP status code as: HTTP/1.1 200 OK
.
Method 3 - cURL Write to stdout
and Fetch http_code
Variable
cURL supports the -w
parameter which allows you to write the response to standard out after completion. This format includes various variables which holds different informations from the response such as content_type, http_version, json, local_ip, etc.
In our case, we are interested in the http_code
variable. To access a given variable using this parameter, use the format as %{variable_name}
For example, to access the http status code:
%{http_code}
An example is as shown:
curl -o /dev/null -s -w "%{http_code}\n" https://www.geekbits.io
200
This should return an output as shown above.
Let us know if you would like us to explore cURL variables and their usage in the comments below or
https://www.geekbits.io/submit-request/
Conclusion
In this article, we explored how to use various methods and techniques to allow cURL to show the HTTP status code from a given request.
We hope you enjoyed this tutorial. Let us know down in the comments.
Thanks for reading!!