cURL: GET request examples
cURL HTTP GET request examples with my most frequently used command-line options.
You can also check my other cURL guides for POST requests, PUT requests, and DELETE requests.
HTTP GET request
The most basic command you can execute with cURL is an HTTP GET request. The following command will perform the request and output the response body:
curl https://blog.marcnuri.comHTTP GET request and follow redirects
Many times when we perform a GET HTTP request, the server responds with a 3xx redirect HTTP status code. Most times, we're not interested on this response, but on the response of the final redirected URL.
To tell cURL to follow redirects we can use the -L, --location command-line option:
curl -L http://blog.marcnuri.comBy default, cURL will follow a maximum of 50 redirects. If this is not enough, the value can be overridden by using the --max-redirs option:
curl -L --max-redirs 60 http://blog.marcnuri.comHTTP GET request and save output to file
To save the output of the response you can either use the -O or -o command-line options.
In case you want to save the command output to a file with the same name as the remote file-name, you can use the -O, --remote-name command-line option (this requires that the request is performed to a remote file):
curl -O https://blog.marcnuri.com/index.htmlIf you want to write the output to a specific file you can use the -o, --output command-line option:
curl -o result.html https://blog.marcnuri.comHTTP HEAD request, retrieving only the response headers
You can use the -I, --head command-line option to instruct cURL to only retrieve the response headers (perform a HEAD HTTP request):
curl -I https://blog.marcnuri.comHTTP GET request with authentication
When accessing protected resources, you'll need to provide authentication credentials. cURL supports several authentication methods.
Basic authentication
For HTTP Basic authentication, use the -u, --user command-line option followed by the username and password separated by a colon:
curl -u username:password https://api.marcnuri.com/protectedBearer token authentication
For APIs that use Bearer tokens (OAuth 2.0, JWT, etc.), you need to add an Authorization header using the -H, --header option:
curl -H "Authorization: Bearer your-token-here" https://api.marcnuri.com/protectedAPI key authentication
Many APIs use API keys passed as headers. The header name varies by API:
curl -H "X-API-Key: your-api-key" https://api.marcnuri.com/dataVerbose output and debugging
When troubleshooting requests, verbose mode shows the full request and response details including headers, SSL handshake, and more.
Use the -v, --verbose command-line option:
curl -v https://blog.marcnuri.comIf you only need the HTTP status code, you can use a combination of options:
curl -o /dev/null -s -w "%{http_code}\n" https://blog.marcnuri.comRequest timeout
To prevent requests from hanging indefinitely, you can set timeouts using the --connect-timeout and --max-time options:
curl --connect-timeout 5 --max-time 10 https://blog.marcnuri.comThe --connect-timeout option limits the time allowed for the connection to be established, while --max-time limits the total time for the entire operation.
Summary
These are some of the common cURL GET HTTP request examples I use on a daily basis. I hope they may come in useful for you too.
