How to Use CURL to Send API Requests

In this article, we’re going to discuss how to use curl to interact with RESTful APIs. curl is a command-line utility that can be used to send requests to an API.

API requests are made up of four different parts:

curl Syntax

The syntax for the curl command is:

curl [options] [URL. ] 

The options we will cover in this post are:

HTTP GET

The GET method is used to fetch a resource from a server. In curl , the GET method is the default method, so we don’t need to specify it.

curl https://jsonplaceholder.typicode.com/posts 

GET With Query Parameters

We can also send query parameters along with the curl GET request.

curl https://jsonplaceholder.typicode.com/posts?userId=5 

HTTP POST

The POST method is used to create a resource on the server.

To send a curl POST request we use the option -X POST .

POST Form Data

curl -X POST -d "userId=5&title=Post Title&body=Post content." https://jsonplaceholder.typicode.com/posts 

By default, curl uses Content-Type: application/x-www-form-urlencoded as the Content-Type header, so we don’t need to specify it when sending form data.

POST JSON

To POST a JSON by curl we have to specify the Content-Type as application/json .

curl -X POST -H "Content-Type: application/json" \ -d '' \ https://jsonplaceholder.typicode.com/posts 

HTTP PUT

The PUT method is used to update or replace a resource on the server. It replaces all data of the specified resource with the supplied request data.

Note: For a PUT request, we have to provide all data in the request body.

To send a curl PUT request we use the option -X PUT .

curl -X PUT -H "Content-Type: application/json" \ -d '' \ https://jsonplaceholder.typicode.com/posts/5 

The above PUT request will replace our previously created post with “New post title” and “New post body”.

HTTP PATCH

The PATCH method is used to make partial updates to the resource on the server.

Note: For a PATCH request, we don't have to provide all data. We only send the data we want updated.

To send a curl PATCH request we use the option -X PATCH .

curl -X PATCH -H "Content-Type: application/json" \ -d '' \ https://jsonplaceholder.typicode.com/posts/5 

Notice how we are only sending the body with “Updated post content” as we are doing a partial update.

HTTP DELETE

The DELETE method is used to remove the specified resource from the server.

To send a curl DELETE request we use the option -X DELETE .

curl -X DELETE https://jsonplaceholder.typicode.com/posts/5 
Note: The DELETE method has no body.

Authentication

Sometimes an API endpoint has restricted access and will only serve requests to authenticated and authorized users. For these requests, we have to provide an access token in the header of the request.

To send a curl header, we use: -H option.

The following request sends POST request with a bearer token in the header:

curl -X POST \ https://some-web-url/api/v1/users \ -H 'Accept: application/json' \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer ' \ -H 'cache-control: no-cache' \ -d '< "username" : "myusername", "email" : "myusername@gmail.com", "password" : "Passw0rd123!" >' 

Conclusion

In this post we learned how to send HTTP requests (GET, POST, PUT, PATCH and DELETE) to an API using curl commands.