How to Send GET Requests Using the cURL Command?

With the cURL command, we can simulate GET requests. The basic syntax is curl [options] [URL], and we can execute GET requests by specifying the request method, URL, and adding request header information.

Home > Blog > How to Send GET Requests Using the cURL Command?

GET request is one of the most common types of requests in the HTTP protocol, used to fetch resources from a server. So, how do we simulate a GET request using the cURL command? Details are provided below.

Basic Syntax and Usage of cURL Command

The basic syntax of the cURL command is curl [options] [URL], where [options] are optional parameters used to configure various options of the request, and [URL] is the target URL to request. By entering different parameter options and URLs in the command line, we can perform various types of requests.

Structure and Characteristics of GET Requests

The primary purpose of a GET request is to retrieve data from the server without making any modifications to the resources on the server. Unlike other types of requests, the parameters of a GET request are typically appended to the end of the URL in the form of a query string.

Steps to Simulate a GET Request Using the cURL Command

1.Specify the request method as GET

Use the -X GET parameter in the command line to specify the request method as GET. Of course, cURL defaults to sending a GET request if the request method is not specified. Therefore, in practical usage, it is usually unnecessary to add the additional -X GET parameter, but you may add it if you prefer.

2.Specify the URL of the request

Append the URL you want to request after the command. For example:

curl -X GET http://127.0.0.1:8000/token

3.Add optional request header information

If necessary, use the -H parameter to add request header information, such as API tokens, user agents, authorization information, etc. For example:

curl -H "Authorization:Bearer %YOUR_TOKEN%" -X GET http://127.0.0.1:8000/token

The %YOUR_TOKEN% here is read from environment variables, or you can directly assign a value here.

4.Execute the request and retrieve the response

Press Enter in the command line to execute the request. cURL will then send the GET request and return the server's response.

How to Send GET Requests Using the cURL Command

Advanced Usage of GET Requests

1.Sending GET requests with parameters using cURL

When we need to send GET requests with parameters, simply add the parameters after the URL, similar to how it's done in a browser. These parameters usually appear in key-value pairs and can be separated by a connector (e.g., &). Our cURL command might look like this:

curl https://api.example.com/users?username=john_doe

This command instructs cURL to send a GET request to the specified URL with the parameter username=john_doe. The server will parse this parameter and return the information of the corresponding user.

2.Handling response data in GET requests

If the server returns data in JSON format, we can use command-line tools like jq (a tool for processing JSON data) to parse and extract the information we need. Suppose we send a GET request to the server to get user information and receive the following JSON-formatted response:

{
  "username": "john_doe",
  "email": "[email protected]",
  "age": 30,
  "city": "New York"
}

We can use the jq command to extract specific information from it. For example, to extract the user's email address, we can use the following command:

curl https://api.example.com/users?username=john_doe | jq '.email'

  This will output:

"[email protected]"

Using this method, we can send GET requests using cURL and handle the server's response data to meet our requirements.

Summary

cURL requests are a common type of request in the HTTP protocol, used to fetch resources from a server. With the cURL command, we can simulate GET requests. The basic syntax is curl [options] [URL], and we can execute GET requests by specifying the request method, URL, and adding request header information. Advanced usage includes sending requests with parameters and handling response data.

How to Send GET Requests Using the cURL Command?
With the cURL command, we can simulate GET requests. The basic syntax is curl [options] [URL], and we can execute GET requests by specifying the request method, URL, and adding request header information.


Learn more:


Learn more: