How to Make REST API Requests Using curl Command?

REST (Representational State Transfer) APIs (Application Programming Interfaces) allow users to send requests to a server to retrieve or modify data. In a REST API, requests are made to a specific endpoint or URL, and the server returns a response. The response could be a message or a complex data structure.

This article will demonstrate various methods to make REST API requests. The content of this article is mentioned below:

How to Make REST API Requests Using Curl Command?

REST APIs use HTTP status codes to display the failure or success of an API request. For better understanding, for example, the “200 OK” status code means the request is successful, and the “404 Not Found” represents the requested resource that is not found.

To make a REST API request, the user can utilize a tool such as a curl. The most common HTTP methods that are used in REST APIs are provided below:

  • GET: It refers to extracting a resource
  • POST: It represents creating a resource
  • PUT: It updates the resource
  • DELETE: It deletes the resource

Here are some examples of how you can use curl to make different types of REST API requests:

Make a GET Request

To retrieve a resource, you can use the curl command with “X” by specifying the “URL”. For this, execute the below script by providing the link “www.itslinuxfoss.com”:

$ curl -X GET https://www.itslinuxfoss.com

The output shows that the “GET” request is made to the link “www.itslinuxfoss.com” for retrieving a resource.

Filter the Retrieved Data

You can also include query parameters in the URL to filter the data you retrieve. It will get the data after the filtration:

$ curl -X GET https:/www.itslinuxfoss.com?param1=value1&param2=value2

The output returns the filtered data “[1] 3308” using the query parameter.

Make a POST Request

To make a POST request for creating a resource; the curl command is used by providing the “www.itslinuxfoss.com” link. Furthermore, the “d” option is used to represent the data having key value, and the “H” option refers to the header to be sent:

$ curl -X POST https://www.itslinuxfoss.com -d '{"key": "value"}' -H 'Content-Type: application/json'

The output shows that the “POST” request has been successfully made to a particular URL.

Make a PUT Request

To make a PUT request for updating a resource; the curl command is utilized by mentioning the “www.itslinuxfoss.com” link. Additionally, users can send data using the “d” option by providing the header via the “H” option:

$ curl -X PUT https://www.itslinuxfoss.com -d '{"key": "value"}' -H 'Content-Type: application/json'

The output shows that the “PUT” request has been successfully made to the “www.itslinuxfoss.com” link.

Make a DELETE Request

To make a DELETE request to delete a resource, you can use the following curl command with the “X” option by specifying the “www.itslinuxfoss.com”:

$ curl -X DELETE https://www.itslinuxfoss.com

That is all from the REST API requests using the curl command.

Conclusion

Linux offers the “curl” command to make REST API requests to the server for retrieving or modifying data. Different HTTP methods include GET to retrieve, POST to create, PUT to update, and DELETE to delete a resource. This guideline has explained all common methods to make REST API requests.