How to Send HTTP Requests via Terminal with Curl

RESTful API Requests from Client to Server via Command Line

by Alexandra Kropova from Mammoth Interactive

In this tutorial

  • GET, POST, PUT, PATCH and DELETE requests
  • This tutorial assumes you have a project set up that handles client-server communication, and you need to send HTTP requests from the client to the server.

Want to learn more? Get 200+ courses and 2,000 hours in the Mammoth Unlimited Membership.

What is curl?

curl is a tool to transfer data from or to a server

Install curl

To check to see if you have curl installed on your computer:

  • Open your Terminal application (also called Command Line or Command Prompt)
  • Enter the command: curl
  • If you see, “Error: curl not found,” install curl.

GET request with curl

What is a GET request?

This type of HTTP request is for retrieving data.

Example of a GET request

Paste the following code into your Terminal (or Command Line) application to make a GET request from http://localhost:3000/api:

curl http://localhost:3000/api

Breaking down the GET request

  • curl is the command to use the curl tool
  • http://localhost:3000/api is the route from which you want to get data. You must replace this with whichever route your project uses to handle a GET request.

Your sever must listen for the client’s request of http://localhost:3000/api. The server’s response must be to display all the data in the database.

Note – Your server must handle each route that you visit, such as http://localhost:3000/api.

Where is the type of request?

The default HTTP request type is GET. Therefore, we don’t have to specify the type of request in the command.

Do more?

You can also use GET to retrieve one piece of data by an identifier, like a unique ID.

POST request with curl

What is POST request?

This type of HTTP request is for creating a new entry in a database.

Example of a POST request

curl -d “name=Alex&course=Unreal” -X POST http://localhost:3000/api/people

Breaking down the POST request

  • curl is the command to use the curl tool

Sending data in the request body

  • -d is the command to specify that the next code will be data (the body of your request).
    • We need to send data containing the properties of the object we want to add to our database.
  • Because we have 2 properties of our object, we enclose them in double quotation marks “”.
    • 1 property of the object is name, which has the value Alex.
    • Another property of the object is course, which has the value Unreal.
    • Properties are separated with an ampersand (‘and’ symbol) &.

Sending the type of request

  • -X is the command to specify that the next code will be the type of request sent.
  • POST is the type of request we are sending, a POST HTTP request.
  • http://localhost:3000/api/people is the route that handles posting data.

Server side

  • Note – Your sever must listen for the client’s POST request of http://localhost:3000/api/people.
  • The server’s response must be to take the data in the body of the request and put it into the project database.

PUT request with curl

What is PUT request?

This type of HTTP request is for updating an entry entirely in the database.

Example of a PUT request

 curl -d “name=Alex&course=Excel” -X PUT http://localhost:3000/api/people/61326891216b79d8768c6edd

Breaking down the PUT request

  • curl is the command to use the curl tool
  • -d is the command to specify that the next code will be data (the body of your request).
    • We need to send data containing the properties of the object we want to add to our database.
  • Because we have 2 properties of our object, we enclose them in double quotation marks “”.
    • 1 property of the object is name, which has the value John.
    • Another property of the object is course, which has the value Excel.
    • Properties are separated with an ampersand (‘and’ symbol) &.
  • -X is the command to specify that the next code will be the type of request sent.
  • PUT is the type of request we are sending, an HTTP PUT request.
  • http://localhost:3000/api/people/:id is the route that handles putting data.
    • You must replace :id with an actual id of an entry in your database, such as 61326891216b79d8768c6edd.
    • An ID is a unique identifier for each entry in your database.
    • The ID value is known as a parameter of the request.

Server side

  • Note – Your sever must listen for the client’s PUT request at http://localhost:3000/api/people/:id.
  • The server must take in the parameter of the request, the ID, and find an entry in the database at that ID.
    • If no entry exists, throw an error.
  • The server’s response must be to take the data in the body of the request and replace the properties of the entry in the database with the new properties.
    • Replace the old name and course Alex and Unreal with John and Excel.

PATCH request with curl

What is PATCH request?

This type of HTTP request is for updating 1 property of an entry in the database.

Example of a PATCH request

  curl -d name=Mammoth -X PATCH http://localhost:3000/api/people/61326891216b79d8768c6edd

Breaking down the PATCH request

  • curl is the command to use the curl tool
  • -d is the command to specify that the next code will be data (the body of your request).
    • We need to send data containing the properties of the object we want to add to our database.
  • 1 property of the object is name, which has the value Mammoth.
  • -X is the command to specify that the next code will be the type of request sent.
  • PATCH is the type of request we are sending, an HTTP PATCH request.
  • http://localhost:3000/api/people/:id is the route that handles patching data.
    • You must replace :id with an actual id of an entry in your database, such as 61326891216b79d8768c6edd.
    • An ID is a unique identifier for each entry in your database.
    • The ID value is known as a parameter of the request.

Server side

  • Note – Your sever must listen for the client’s PATCH request at http://localhost:3000/api/people/:id.
  • The server must take in the parameter of the request, the ID, and find an entry in the database at that ID.
    • If no entry exists, throw an error.
  • The server’s response must be to take the data in the body of the request and replace the property of the entry in the database with the new property.
    • Replace the old name John with the new name Mammoth.

DELETE request with curl

What is DELETE request?

This type of HTTP request is for deleting an entry in a database.

Example of a DELETE request

  curl -X DELETE http://localhost:3000/api/people/61325f4594bd88dec1e165cb

Breaking down the DELETE request

  • curl is the command to use the curl tool
  • -X is the command to specify that the next code will be the type of request sent.
  • DELETE is the type of request we are sending, an HTTP DELETE request.
  • http://localhost:3000/api/people/:id is the route that handles deleting data.
    • You must replace :id with an actual id of an entry in your database, such as 61326891216b79d8768c6edd.
    • An ID is a unique identifier for each entry in your database.
    • The ID value is known as a parameter of the request.

Server side

  • Note – Your sever must listen for the client’s DELETE request at http://localhost:3000/api/people/:id.
  • The server must take in the parameter of the request, the ID, and find an entry in the database at that ID.
    • If no entry exists, throw an error.
  • The server’s response must be to take the data in the body of the request and delete it from the database.

Want to learn more? Get 200+ courses and 2,000 hours in the Mammoth Unlimited Membership.

  • 20-60 hours added every month.
  • On sale now.
  • Don’t miss out!
Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

EMAIL US

support@mammothinteractive.com