How to use curl for HTTP request types

Alex.Muriuki
4 min readJan 4, 2022

--

HTTP client and webserver

Curl basics

What is curl: curl is command-line tool to transfer data to and from the server

GET request using curl

$curl http://localhost:3000/api

curl is the command to use the curl tool

http://localhost:3000/api is the route from which we want to get data

The default curl http request is GET so we do not have to specify the the type of request in the command

NOTE:

your server must listen for the client’s GET request at http://localhost:3000/api and the server’s response must be to display all data in the database

your server must handle each route you visit such as http://localhost:3000/api

you can also use GET to retrieve one piece of data using a unique identifier, like an ID eg. http://localhost:3000/api/users/61326891216b79d8768c6edd

POST request using curl

$curl -d “name=Alex&age=34” -X POST http://localhost:3000/api/users

curl is the command to use the curl tool

-d is the command to specify that the next piece of code is data

because we have more than one properties of our object to post in our DB we use quotation marks “”

ie. “name=Alex&age=34” properties are separated using &

-X is the command to specify the type of HTTP request sent

in this case we are sending a POST HTTP request

http://localhost:3000/api/users is the route that handles posting data

NOTE:

your server must listen for the client’s POST request at http://localhost:3000/api/users

your server must take in the parameter of the request, the id and find an entry in the database with that id. If no entry exists, return an error

your server’s response must be to take the data in the body of the request and place it in the database

PUT request using curl

PUT is a request for updating an entry entirely in the database

$curl -d “name=KimPossible&age=56” -X PUT http://localhost:3000/api/users/61326891216b79d8768c6edd

curl is the command to use the curl tool

-d is the command to specify that the next piece of code will be the data

because we have more than one properties of our object to post in our DB we use quotation marks “”

ie. ”name=Alex&age=35” properties are separated using &

-X is the command to specify the type of HTTP request

in this case we are sending a HTTP PUT request

http://localhost:3000/api/users/:id is the route that handles putting data

replace :id with the actual id of the entry in your database update entirely

id value is known as the parameter of the request

NOTE:

your server must listen for the client’s PUT request at http://localhost:3000/api/users/:id

your server must take in the parameter of the request, the id and find an entry in the database with that id. If no entry exists, return an error

your server’s response must be to take the data in the body of the request and update it entirely with the new data, in this case update old name Alex with new name KimPossible and old age 34 with new age 56

PATCH request using curl

PATCH is a request for updating one property of an entry in the database

$curl -d name=John -X PATCH http://localhost:3000/api/users/61326891216b79d8768c6edd

curl is the command to use the curl tool

-d is the command to specify that the next piece of code will be data

and the property sent is only one with value of John

-X is the command to specify the type of HTTP request

in this case we are sending a HTTP PATCH request

http://localhost:3000/api/users/:id is the route that handles patching data

replace :id with the actual id of the entry in the database to update

the id is the unique identifier for each entry in your database

id value is known as the parameter of the request

NOTE:

your server must listen for the client’s PATCH request at http://localhost:3000/api/users/:id

your server must take in the parameter of the request, the id and find an entry in the database with that id. If no entry exists, return an error

your server’s response must be to take the data in the body of the request and replace it with the new property, in this case replace old name KimPossible with new name John

DELETE request using curl

DELETE is a request for deleting an entry in the database

$curl -X DELETE http://localhost:3000/api/users/61326891216b79d8768c6edd

curl is the command to use the curl tool

-X is the command to specify that the next piece of code is the HTTP request type

in this case the HTTP request is HTTP DELETE request

http://localhost:3000/api/users/:id is the route that handles deleting data

replace :id with the actual entry of the id in the database in this example the id used is 61326891216b79d8768c6edd

the id value is known as the parameter of the HTTP request

NOTE:

your server must listen for the client’s DELETE request at http://localhost:3000/api/users/:id

your server must take in the parameter of the request, the id and find an entry in the database with that id. If no entry exists, return an error

your server’s response must be to take the data in the body of the request and delete it from the database

congrats you now know how to use curl now to make restful API requests from the command-line

Congrats! You now know how to use curl to make RESTful API requests from client to server via the command line.

--

--

Alex.Muriuki

Am a software engineer based in Nairobi, Kenya. Here is where I document my learning.