Curl Command - Transfer Data & Make API Requests | Online Free DevTools by Hexmos

Learn to use the curl command for transferring data and making REST API requests. Explore GET, POST, PUT, DELETE methods with practical examples.

Curl Command

Curl: Data Transfer and API Interaction

The curl command is a powerful command-line tool used for transferring data from or to a server. It supports various protocols, making it indispensable for developers for tasks ranging from downloading files to interacting with RESTful APIs.

The basic syntax for curl is:

curl [options] [URL]

Common Curl Options

Option Description
-o Save the output to a file with a specified name.
-O Save the output to a file using the same name as the remote file.
-C - Resume a download from where it left off.
-I Fetch only the HTTP headers of a URL.
-L Follow HTTP redirects.

Curl Usage Examples

  • Retrieve a webpage's content:
    curl example.com
  • Save a webpage to a local file:
    curl example.com -o example.html
  • Resume an interrupted download:
    curl -C - -O https://releases.ubuntu.com/21.10/ubuntu-21.10-desktop-amd64.iso
  • Fetch only the headers of a URL:
    curl -I example.com
  • Get weather information for a location:
    curl wttr.in/london

Handling REST API Calls with Curl

curl is widely used for making HTTP requests to interact with RESTful APIs. Here's how to perform common HTTP methods:

GET Requests

The GET method is used to fetch resources from a server. It is the default method in curl, so you often don't need to specify it explicitly.

Example of a GET request:

curl https://reqres.in/api/users/2

POST Requests

The POST method is used to create a new resource on the server. You typically send data in the request body.

To send a curl POST request, use the -X POST option and specify the content type and data using -H and -d respectively.

Example of a POST request to register a user:

curl -X POST -H "Content-Type: application/json" -d '{"email": "eve.holt@reqres.in","password": "pistol"}' https://reqres.in/api/register

PUT Requests

The PUT method is used to update an existing resource on the server. Similar to POST, you send data in the request body.

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

Example of a PUT request to update user information:

curl -X PUT -H "Content-Type: application/json" -d '{"name": "morpheus","job": "zion resident"}' https://reqres.in/api/users/2

DELETE Requests

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

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

Example of a DELETE request:

curl -X DELETE https://reqres.in/api/users/2

For more advanced usage and options, refer to the official curl documentation.