Sending HTTP requests is a fundamental aspect of web development and API interaction. One popular command line tool for making these requests is Curl. HTTP GET requests are used to retrieve data from a specified resource, such as a webpage or an API endpoint. This tutorial demonstrates how to send a GET request using Curl.
Simple GET request
To perform a simple GET request, pass a target URL as first argument, as follows:
curl https://httpbin.org/get
In this scenario, the output you'll receive will be in JSON format. It will look similar to the following:
{
"args": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.81.0",
"X-Amzn-Trace-Id": "Root=1-6416c45e-47a4828a557d5fca764271d6"
},
"origin": "XXX.XXX.XXX.XXX",
"url": "https://httpbin.org/get"
}
Add query parameters
If the URL requires query parameters, you can append them directly to the URL:
curl "https://httpbin.org/get?param1=value1¶m2=value2"
Make sure to wrap the URL in double quotes to prevent issues with special characters.
View response headers
If you're interested in viewing only the response headers, you can use the -I
option:
curl -I https://httpbin.org/get
The output could look something like this:
HTTP/2 200
date: Sat, 26 Aug 2023 04:12:54 GMT
content-type: application/json
content-length: 256
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true
This can be useful to check headers like content type and server information.
Save output
To save the output of the GET request to a file, you can use the -o
option:
curl -o response.json https://httpbin.org/get
This will save the retrieved content to a file named response.json
.
Leave a Comment
Cancel reply