Logo AppDev24 Login / Sign Up
Sign Up
Have Login?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Login
New Account?
Recovery
Go to Login
By continuing you indicate that you agree to Terms of Service and Privacy Policy of the site.
Shell Script

Response Handling using cURL

 
Updated on Jun 22, 2024

cURL (Client URL) is a powerful tool that allows us to send HTTP requests and view the responses in a human-readable format. With cURL, we can simulate various types of requests, such as GET, POST, PUT, and DELETE, and examine the response headers, codes, and bodies.

Response Header

When making an API request, it's essential to inspect the response header to gather information about the request. For example, we might want to know the server type, content type, or cache control directives. To view the response header using cURL, run the following command:

curl -s -I -X GET https://jsonplaceholder.typicode.com/todos/1

The -s option tells cURL to be silent and not display the progress meter, while the -I option instructs cURL to show only the headers. The GET request is made to the specified URL, which returns a JSON response.

Response Code

Another crucial aspect of response handling is examining the response code, also known as the HTTP status code. This code indicates whether the request was successful or not. To view the response code using cURL, run the following command:

curl -s -o /dev/null -w "%{http_code}\n" -X GET https://jsonplaceholder.typicode.com/todos/1

The -o option tells cURL to write the output to a file (in this case, /dev/null, which discards the output), while the -w option specifies the format of the output. The "%{http_code}\n" format displays only the HTTP status code.

Response Body

Finally, we might want to inspect the response body, which contains the actual data returned by the API or web server. To view the response body using cURL, run the following command:

curl -s -H "Content-Type: application/json" -X GET https://jsonplaceholder.typicode.com/todos/1

The -H option sets a custom header (in this case, Content-Type) to specify that you want to receive JSON data. The response body is then displayed in a human-readable format.

In conclusion, understanding how to handle responses from APIs and web servers using cURL is an essential skill for any developer. By mastering the basics of response handling, we can troubleshoot issues, inspect API responses, and build more robust applications.

PrimeChess

PrimeChess.org

PrimeChess.org makes elite chess training accessible and affordable for everyone. For the past 6 years, we have offered free chess camps for kids in Singapore and India, and during that time, we also observed many average-rated coaches charging far too much for their services.

To change that, we assembled a team of top-rated coaches including International Masters (IM) or coaches with multiple IM or GM norms, to provide online classes starting from $50 per month (8 classes each month + 4 tournaments)

This affordability is only possible if we get more students. This is why it will be very helpful if you could please pass-on this message to others.

Exclucively For Indian Residents: 
Basic - ₹1500
Intermediate- ₹2000
Advanced - ₹2500

Top 10 Articles