Table of Contents
Using cUrl command with -w
, -s
and -o
options
For Get Method
Use the -w
or --write-out
option in combination with the %{http_code}
format specifier to get curl response code in Bash for get method.
1 2 3 4 5 |
#!/bin/bash response_code=$(curl -s -o /dev/null -w %{http_code} 'https://www.google.com') echo "Response code: $response_code" |
1 2 3 |
Response code: 200 |
We can also rewrite it into more verbose format in case you need for better understanding.
1 2 3 4 5 |
#!/bin/bash response_code=$(curl --silent --output /dev/null --write-out %{http_code} 'https://www.google.com') echo "Response code: $response_code" |
In the above example, -w
is used to get the HTTP response code for the URL https://www.google.com
, which is returned as 200
, which indicates the successful request. The curl
is a command line tool to make HTTP requests.
Here, the -s
parameter means silent mode; it prevents the curl from showing progress and error messages. After that, -o /dev/null
redirects the response body to /dev/null
to discard it and only focus on extracting the response code. This is done because we want to get only the response code from the server, not the response that includes both the response code and the response body.
Then, -w
is used to specify the output format. Where "%{http_code}"
is a curl format specifier representing the HTTP response code. Finally, the response code is stored in the response_code
variable, displayed on the screen using the echo
command.
Replace the ‘https://www.google.com‘ with your desired URL.
For POST
Request
Use the -X POST
option to specify the HTTP method as POST
to obtain the curl response code from a POST
request in bash.
1 2 3 4 5 |
#!/bin/bash response_code=$(curl -s -o /dev/null -w "%{http_code}" -X POST 'https://example.com/api' -d "data=value") echo "Response code: $response_code" |
1 2 3 |
Response code: 404 |
In the above bash snippet, we obtained the curl response code from the POST
request for the URL 'https://example.com/api'
. Here, -X POST
represents the HTTP method as POST
. Moreover, the -d "data=value"
option is used to be sent in the request body. The remaining code resembles the previous example, where the response code is stored in response_code
and then displayed.
Replace the ‘https://www.google.com‘ with your desired URL.
We can observe, on the execution of this code, the 404
response code is returned that represents the resource "Not Found"
on the server. In other words, the server is unable to locate the given URL.
For PUT
Request
Use the -X PUT
option to specify the HTTP method as PUT
to obtain the response code from a PUT
request using curl in bash.
1 2 3 4 5 |
#!/bin/bash response_code=$(curl -s -o /dev/null -w "%{http_code}" -X PUT 'https://example.com/api' -d "data=value") echo "Response code: $response_code" |
1 2 3 |
Response code: 404 |
The -X PUT
option retrieves the curl response code from a PUT
request in the above example. Similar to the POST
example, you can include the data to be sent in the request body using the -d "data=value"
option. The rest of the command remains the same.
For DELETE
Request
Use the -X DELETE
option to specify the HTTP method as DELETE
to obtain the curl response code from a DELETE
request.
1 2 3 4 5 |
#!/bin/bash response_code=$(curl -s -o /dev/null -w "%{http_code}" -X DELETE'https://example.com/api' ) echo "Response code: $response_code" |
1 2 3 |
Response code: 404 |
In this bash code, we obtained the curl response code from the DELETE
request for the URL 'https://example.com/api'
. Here, there is no need to send additional data. The rest of the command is the same as above.
For HEAD
Request
Use the -I
option to retrieve only the header and exclude the response body to obtain the curl response code from a HEAD
request.
1 2 3 4 5 |
#!/bin/bash response_code=$(curl -s -o /dev/null -w "%{http_code}" -I 'https://google.com') echo "Response code: $response_code" |
1 2 3 |
Response code: 301 |
The only difference in this example is -I
, which is used for head requests and the rest of the example is the same. Here, response code 301
indicated permanent redirection, which means the requested resource is moved to another address permanently.
That’s all about Bash get curl response code.