curl GET Request
Learn how to make HTTP GET requests with curl. Understand query parameters, response headers, and output options for retrieving data from any API endpoint.
Detailed Explanation
curl GET Request Basics
The GET request is the most fundamental HTTP method, used to retrieve data from a server. In curl, a GET request is the default behavior when no other method is specified.
Basic Syntax
curl https://api.example.com/users
This sends a simple GET request and prints the response body to standard output. curl automatically sets the User-Agent header and handles DNS resolution, TCP connection, and TLS negotiation behind the scenes.
Adding Query Parameters
Query parameters can be appended directly to the URL or passed using the --data-urlencode flag with -G:
curl -G https://api.example.com/search \
--data-urlencode "q=hello world" \
--data-urlencode "page=1"
The -G flag ensures the data is sent as URL query parameters rather than in the request body.
Viewing Response Headers
Use -i to include response headers in the output, or -I to fetch headers only (HEAD request):
curl -i https://api.example.com/users
Saving the Response
Redirect the output to a file with -o or use -O to save with the remote filename:
curl -o response.json https://api.example.com/users
Common Options for GET Requests
-s(silent): Suppress the progress bar-S(show errors): Show errors even in silent mode-f(fail): Return an error code on HTTP errors-L(follow redirects): Automatically follow 3xx redirects
Combining these into a robust GET request pattern:
curl -sSfL https://api.example.com/users
Understanding GET requests in curl is essential because they form the basis for converting to other languages like Python, JavaScript, or Go.
Use Case
A developer testing a REST API endpoint during development needs to quickly verify that the server returns the expected JSON response before writing application code.