Important curl command flags

Flag

Description

Example

Verbose Mode (-v)

Gain insights into the request-response process with detailed information by enabling verbose mode.

curl -v https://example.com

Silent (-s)

Don’t show progress meter or error messages. Makes Curl mute.

curl -s https://example.com

Output to File (-o)

Save the API response to a file for further analysis using the -o flag followed by the desired filename.

curl -L ip.ba3a.tech -o ip.json

Authentication

Curl supports various authentication mechanisms, allowing secure access to protected APIs. Use -u for basic authentication.

curl -u user:password https://example.com

Error Handling (-w %{http_code})

Capture the HTTP status code returned by the API using the -w %{http_code} flag. Helps identify errors (e.g., 404 Not Found, 500 Internal Server Error).

curl -w %{http_code} https://example.com

Using Curl to make REST API requests

REST APIs are essential for modern web applications, enabling programmatic interaction with data and functionality. Curl is a command-line tool for making web requests, often used directly from the terminal. For example, curl -L ip.ba3a.tech fetches IP address details in JSON format, just like visiting the site in a browser. Essentially, Curl prints the web response to the terminal. In this article, we will learn how to make REST API requests using Curl.

Using curl to make REST API requests

  • Using curl to make REST API requests
    • Example 1: Create (POST) Request
    • Example 2: Read (GET) Request
    • Example 3: Update (PUT) Request
    • Example 4: Delete (DELETE) Request
  • Important curl command flags

For the sake of this article, let’s use a sample social media API that I have created. This will help demonstrate how to make REST API requests using cURL.

Example 1: Create (POST) Request

In this example, we use curl to send a POST request to the endpoint https://crud.ba3a.tech/users to create a new user. The -X POST flag specifies the request method, -H “Content-Type: application/json” sets the content type header, and -d ‘{“name”: “Ba3a”, “email”: “someMail@gmail.com”}’ sends the user data in JSON format.

Command:

#!/bin/bash

curl -X POST \
-H "Content-Type: application/json" \
-d '{"name": "Ba3a", "email": "someMail@gmail.com"}' \
https://crud.ba3a.tech/users

Explanation:

  • -X POST: Specifies the HTTP method for creating a new resource.
  • -H “Content-Type: application/json”: Sets the content type header to indicate JSON data is being sent.
  • -d ‘{“name”: “Ruchi”, “email”: “someMail@gmail.com”}’: Includes the user data in JSON format using the -d flag.

Output:

Create (POST) Request

Example 2: Read (GET) Request

In this example, we use curl to send a GET request to the endpoint https://crud.ba3a.tech/users/1 that fetches user data based on the user-id i.e. 1.

Command:

curl https://crud.ba3a.tech/users/1

Explanation:

  • curl: Command-line tool for transferring data using various network protocols. In this context, it’s used to make an HTTP GET request.
  • https://crud.ba3a.tech/users/1: The URL to which the GET request is sent.

Output:

Create Read (GET) Request

Example 3: Update (PUT) Request

In this example, we use curl to send a PUT request to the endpoint https://crud.ba3a.tech/users/3 to update the user. The -X PUT flag specifies the request method, -H “Content-Type: application/json” sets the content type header, and -d ‘{“email”: “updated.email@example.com”}’ sends the user data in JSON format.

Command:

#!/bin/bash

curl -X PUT \
-H "Content-Type: application/json" \
-d '{"email": "updated.email@example.com"}' \
https://crud.ba3a.tech/users/3

Explanation:

  • curl: This is the command-line tool used to transfer data with URLs.
  • -X PUT: Specifies the HTTP method to use, in this case, PUT, which is used to update a resource on the server.
  • -H "Content-Type: application/json": Sets the Content-Type header to application/json, indicating the data format being sent.
  • -d '{"email": "updated.email@example.com"}': Specifies the data to be sent in the request body. Here, it is a JSON object updating the email field.
  • https://crud.ba3a.tech/users/3: The URL endpoint for the API request.

Output:

Create Update (PUT) Request

Example 4: Delete (DELETE) Request

In this example, we use curl to send a DELETE request to the endpoint https://crud.ba3a.tech/users/3 to delete the user with ID 3. The -X DELETE flag specifies the request method.

Command:

curl -X DELETE https://crud.ba3a.tech/users/3

Explanation:

  • curl: This is the command-line tool used to transfer data with URLs.
  • -X DELETE: Specifies the HTTP method to use, in this case, DELETE, which is used to remove a resource on the server.
  • https://crud.ba3a.tech/users/3: The URL endpoint for the API request.

Output:

Create Delete (DELETE) Request

Now that we know how to make REST API requests with CURL, let us see some important flags that might be useful.

Similar Reads

Important curl command flags

Flag Description Example Verbose Mode (-v) Gain insights into the request-response process with detailed information by enabling verbose mode. curl -v https://example.com Silent (-s) Don’t show progress meter or error messages. Makes Curl mute. curl -s https://example.com Output to File (-o) Save the API response to a file for further analysis using the -o flag followed by the desired filename. curl -L ip.ba3a.tech -o ip.json Authentication Curl supports various authentication mechanisms, allowing secure access to protected APIs. Use -u for basic authentication. curl -u user:password https://example.com Error Handling (-w %{http_code}) Capture the HTTP status code returned by the API using the -w %{http_code} flag. Helps identify errors (e.g., 404 Not Found, 500 Internal Server Error). curl -w %{http_code} https://example.com...

Frequently Asked Questions on Using Curl to make REST API requests

Is curl secure for making API requests?...

Conclusion

In conclusion, cURL is a versatile and powerful tool for making REST API requests, enabling efficient interaction with web services directly from the command line. Understanding its various options and flags allows for robust handling of different API operations, from data retrieval to resource updates and deletions....

Contact Us