Making GET requests with cURL: A practical guide (with examples)

Updated: February 1, 2024 By: Guest Contributor Post a comment

Overview

If you’ve ever needed to interact with APIs or fetch resources over the web from a command line or a script, you’re likely familiar with cURL. cURL is a powerful tool that transfers data to or from a server using various protocols, including HTTP(S), FTP, SMTP, and more. In this tutorial, we’ll focus specifically on performing GET requests using cURL.

What is cURL?

cURL, which stands for Client for URLs, is a command-line tool and library for transferring data with URLs. With its simple syntax and broad capabilities, cURL is an invaluable resource for developers to test APIs, download files, and interact with web services.

cURL Options

As you can see from the above examples, cURL offers a variety of options to customize your request. Here’s a shortlist of other useful flags you might use with cURL:

  • -i: Include the response headers in the output
  • -o: Write output to a file, specifying a filename
  • -C -: Continue a previous file transfer from where it stopped
  • --data: Sends the specified data with a POST request

GET Requests

A GET request is used to request data from a specified resource. GET is one of the most common HTTP methods and is used to retrieve data that can be cached and remains unchanged over successive calls.

Using cURL for GET Requests

Using cURL to make a GET request is straightforward. The most basic form of a GET request with cURL is as follows:

curl https://api.slingacademy.com

The above command retrieves data from the specified URL. Now let’s delve into practical examples and explore cURL’s capabilities through various use cases.

Example 1: Basic GET Request

curl http://api.slingacademy.com/v1/sample-data/photos

This command retrieves the post with an ID of 1 from the JSON Placeholder API, which is a test API used for practicing API interaction.

Example 2: Adding Query Parameters

curl http://jsonplaceholder.typicode.com/posts?userId=1

The above command appends a query parameter to filter the results based on the userId. In this case, it fetches posts written by the user with an ID of 1.

Example 3: Including Headers in the Request

curl -H "Accept: application/json" http://jsonplaceholder.typicode.com/posts

The -H option allows you to include headers in your request. Here, we’re specifying that the server should return JSON-formatted data.

Example 4: Saving Output to a File

curl http://jsonplaceholder.typicode.com/posts/1 > post_1.json

By directing the output to a file, you can store the response for further analysis or processing. This example saves the data to a file named ‘post_1.json’.

Example 5: Using Verbose Mode

curl -v http://jsonplaceholder.typicode.com/posts/1

The -v flag (verbose) provides detailed information about the request and response, including headers and HTTP status codes. This is useful for debugging.

Example 6: Using Authentication

curl -u "username:password" http://jsonplaceholder.typicode.com/posts

This command sends a GET request with basic HTTP authentication. Replace ‘username’ and ‘password’ with your credentials. Caution: this sends your credentials in plain text unless over HTTPS.

Example 7: Follow Redirects

curl -L http://jsonplaceholder.typicode.com/posts

The -L flag tells cURL to follow any redirects. If the resource has been moved, cURL will retrieve it from the new location.

Example 8: Custom User Agent

curl -A "MyUserAgent" http://jsonplaceholder.typicode.com/posts

Some servers check the user agent to behave differently for various clients. With -A, you can specify a custom user agent string.

Conclusion

cURL is a highly versatile tool for making HTTP requests from the command line. It’s widely supported and used for anything from simple data retrieval to testing complex APIs. Once you’re familiar with its syntax and the power of its options, you’ll likely find it indispensable for your development or system administration tasks. Remember always to use secure practices, such as transferring sensitive data over HTTPS and storing credentials securely.