curl rest api

cURL Command to Call REST API

cURL is a useful command that allows you to send requests in different ways. It supports multiple protocols, supports authentication, file downloads and data transfers. Sometimes you may need to call REST API using cURL command. In this article, we will learn the cURL command to call rest API.


cURL Command to Call REST API

You may need to use cURL command to call REST API if you want to test the API endpoints, or use the API response in your code. In such cases, you can use cURL command to get API response data and process it further. We will look at different use cases to work with REST API using cURL.


Verbose mode

If you want to test REST API using cURL, use the verbose option -v to make the API call. cURL will display all the progress helping you understand what is going on under the hood. Here is an example to make a call to API endpoint https://example.com/api-endpoint

$ curl -v https://example.com/api-endpoint


Output Mode

The above command will not only display progress but also the response data to standard output. If you want to send the response data to an output file, then use -o option followed by the filename where you want to store the output.

$ curl -o data.json https://example.com/api-endpoint

This is really useful when you expect large response, or you want to save the response data for later use. It also helps you avoid making repeated API calls just to analyze the response data.


Using GET Method

You can also specify request method while making a cURL call to REST API. If you don’t specify any method, by default, cURL will send a GET request.

$ curl -v https://example.com/api-endpoint


Using POST Method

If you want to send POST method, just send the post data using -d option.

$ curl -d 'id=9&name=john' http://example.com/api-endpoint

If you want to upload a file to REST API endpoint, you can specify the filename with @ sign as shown below, and using -H option to set request header.

$ curl -d @input.json -H "Content-Type: application/json" 
https://example.com/api-endpoint

Alternatively, you can send JSON input data in your cURL request.

$ curl -d '{"id":9,"name":"john"}' -H 'Content-Type: application/json' 
https://example.com/api-endpoint

You can use these commands to send access tokens and other details.



Using DELETE Method

If you want to use DELETE method, use -DELETE option as shown below.

$ curl -DELETE https://example.com/api-endpoint


Using Custom Headers

If you want to set custom headers, you can use -H option as shown below.

$ curl -H "Host: mysite.com" http://example.com/api-endpoint

In this article, we have learnt several different ways to use cURL command with REST APIs. You can modify them depending on your requirements.

Also read:

How to Remove WWW from URL in Apache
How to Delete Git Tags
How to Drop One or More Column in Python Pandas
Types of Testing in Python
How to Generate SSH Keys for Git Authorization

Leave a Reply

Your email address will not be published.