How to Make a GET Request with Curl
This article provides a straightforward guide on how to perform a simple HTTP GET request using curl, a popular command-line tool for transferring data over various network protocols. You will learn the basic command syntax, how to save the output to a file, how to view response headers, and how to handle redirects.
The Basic GET Request
By default, curl performs an HTTP GET request when you provide a URL. Open your terminal and run the following command:
curl https://api.github.comThis command sends a GET request to the specified URL and prints the raw response body directly to your terminal.
Saving the Output to a File
If the response is large or you want to save the data for later use,
you can write the output to a file using the -o or
-O options.
- Using lowercase
-oallows you to specify a custom filename:
curl -o response.json https://api.github.com- Using uppercase
-Osaves the file using the filename from the URL:
curl -O https://example.com/index.htmlViewing Response Headers
To inspect the HTTP response headers (such as Content-Type, Status
Code, and Server information) instead of the response body, use the
-I (or --head) option:
curl -I https://example.comIf you want to view both the headers and the response body together,
use the lowercase -i option:
curl -i https://example.comFollowing Redirects
If the URL you are requesting redirects to another location, curl
will not follow it by default. To force curl to follow HTTP redirects,
add the -L (or --location) flag:
curl -L http://google.com