cURL is a command-line tool and a library that allows you to transfer data to and from servers using various protocols. Besides extracting public data, which can be HTML, text, JSON, images, etc., cURL can also handle sessions and cookies. Follow this guide to learn how to send and manage cURL GET requests.
For more details, check out the full article on our website.
If the default request method is GET, you can skip --request
option and send a GET request as follows:
curl https://httpbin.org/get
A GET request with parameters allows you to send additional data to the server within the URL of your request. cURL provides two powerful options, -d
and -G
, to facilitate this.
Note that if you use -d
without -G
, the request will be a POST request. Also, if you use the -X
option with GET value, the data you want to send with -d
will be ignored.
Therefore, you must use -G
along with -d
if you want to send parameters with a GET request.
curl -G -d "param1=value1" -d "param2=value2" https://httpbin.org/get
In the above command, 'param1'
and 'param2'
are the keys, and 'value1'
and 'value2'
are their respective values. The -d
option can be used multiple times to send various parameters.
Alternatively, the GET parameters can be included as part of URL:
curl 'https://httpbin.org/get?param1=value1¶m2=value2'
HTTP headers allow for exchanging additional information between the client and server during an HTTP request. To get the HTTP headers along with the response body, use the -i
or --include
option in the curl GET command:
curl -i https://httpbin.org/headers
This command retrieves the HTTP response headers, such as the server, date, content type, and content length. They provide useful information about the nature and specifics of the response data.
Note that the long parameter for fetching response headers is --head
:
curl --head https://httpbin.org/headers
JSON has become a standard for data exchange in the modern web development ecosystem. When interacting with APIs via cURL, requesting data in JSON format is crucial. You can instruct cURL to accept the response in JSON format by using the -H
option followed by "Accept: application/json"
:
curl -H "Accept: application/json" https://httpbin.org/get
Note that sending "Accept: application/json"
doesn't guarantee that the response will be returned in JSON format. **It highly depends on whether the website supports returning a JSON response.
**
In certain scenarios, the URL you're requesting might redirect to another URL. By default, cURL doesn't follow such redirects, but you can explicitly instruct it to do so. You can achieve this by using the -L
or --location
option:
curl -L 'https://httpbin.org/redirect-to?url=https://httpbin.org/get'
Sometimes, you may need to send cookies with your GET request, especially when interacting with websites that require user sessions or tracking user activity. You can use the -b
or --cookie
option followed by the name and value of the cookie:
curl -b "username=John" https://httpbin.org/cookies
To learn how to send GET requests with cURL, we've prepared a table that shows some key arguments, the short option, the long option, and the description.
Important
Note that you must use either the short or long option, not both.
Argument | Short Option | Long Option | Description |
---|---|---|---|
-Headers | -I |
--head |
Retrieves HTTP headers only. |
Include Headers | -i |
--include |
Includes the HTTP response headers in the output. |
User Agent | -A |
--user-agent |
Specifies the User-Agent string to send to the server. |
Request Type | -X |
--request |
Specifies the request type to use (GET, POST, PUT, DELETE, etc.) |
Follow Redirects | -L |
--location |
Follows redirects in the server's response. |
Send Cookies | -b |
--cookie |
Sends cookies from a string or file. Format of string should be NAME=VALUE; another=anotherval. |
Verbose Mode | -v |
--verbose |
Provides more information (debug info). |
Silent Mode | -s |
--silent |
Silent mode. Don't output anything. |
Output to File | -o |
--output |
Writes output to <file> instead of stdout. |
Pass a Custom Header | -H |
--header |
Passes a custom header to the server. To get JSON, use -H "Accept: application/json" . |