How to Make a GET Request With cURL: The Ultimate Guide

In this guide, I will focus on how to make a GET request with cURL. Curl is a command-line utility that can be used to send and receive HTTP requests. In this article, we will focus on using cURL to receive GET requests and explore various aspects of cURL usage.

What is cURL?

cURL is an open-source command-line tool that transfers data from or to a server using protocols such as HTTP, HTTPS, FTP, FTPS, etc. It is designed to be a straightforward tool that can be used for various purposes, such as debugging, automation, testing, and measuring website performance. The best part about cURL is that it is available on almost every operating system, making it accessible and easy to use.

Why Use cURL for API Requests?

cURL is an ideal tool for testing and measuring API response times. When you make a GET request with cURL, it sends an HTTP request to the server, which returns a response. The time taken to receive a response is known as the response time. By measuring the response time, you can identify any bottlenecks or issues in your API, which can help you optimize your API and improve website performance. Additionally, cURL allows you to see the HTTP headers, cookies, and other metadata, which can help you debug any issues with your API.

Prerequisites

Before we dive into the details of making a GET request with cURL, you need to ensure that you have cURL installed on your system. You can check if cURL is installed by running the following command:

curl --version

If cURL is not installed, you can install it by following the instructions on the official cURL website. Making a GET request with cURL To make a GET request with cURL, you need to use the following syntax:

curl [options] [URL]

Where [options] are the various options you can pass to cURL, and [URL] is the URL to which you want to request GET.  

Installing cURL

cURL is usually available by default on most Unix-based systems, such as macOS and Linux. However, you might need to install it manually on Windows.

Installation on Windows

To install cURL on Windows, visit the official cURL download page (https://curl.se/windows/) and download the appropriate version for your system. Extract the downloaded archive and add the extracted folder to your system’s PATH environment variable.

Installation on macOS

On macOS, cURL is pre-installed by default. You can verify its installation by running curl --version in the Terminal.

Installation on Linux

Step 1: Open the Terminal The first step is to open the Terminal. You can do this by pressing Ctrl + Alt + T on your keyboard or by clicking on the terminal icon in your taskbar.

Step 2: Update the package index. Before we proceed to install Curl, we need to update the package index to make sure we get the latest version of Curl. Type the following command in the Terminal:

sudo apt-get update

Step 3: Install Curl. Once the package index is updated, we can now proceed to install Curl. Type the following command in the Terminal:

sudo apt-get install curl

Step 4: Verify the Installation To verify that Curl is installed on your Linux machine, you can type the following command in the Terminal:

curl --version

This command will display the version of Curl installed on your machine.

How to Make a GET Request With cURL?

The most basic GET request that you can make using cURL is:

curl https://example.com

It may be obvious, but I’ll say it anyway: replace https://example.com/api/endpoint with the URL of the API endpoint you want to test. This command will send a GET request to the API endpoint and return the response.

Passing Query Parameters

In many cases, you may need to pass query parameters to the URL to which you are making a GET request. Query parameters are key-value pairs that are appended to the URL after a ? character. To pass query parameters using cURL, you can simply append them to the end of the URL. For example:

curl https://example.com/api?genius=geek&baz=qux

This will send a GET request to https://example.com/api with the query parameters foo=bar and baz=qux.

HTTP Headers

When working with APIs, tailoring your cURL GET request for optimum performance is essential. One way to achieve this is by adding headers to your request, allowing you to send additional information to the server. In this guide, we’ll walk you through the process of adding headers to a cURL GET request, enabling you to make the most of your API interactions.

Step 1: Define Your Custom Headers

Before incorporating headers into your cURL GET request, determine the specific headers you need. Some common headers include:

  1. Authorization: Authentication credentials, such as API keys or access tokens
  2. Accept: The content type the client accepts (e.g., application/json)
  3. Content-Type: The media type of the request body (e.g., application/x-www-form-urlencoded)
  4. User-Agent: Information about the client software (e.g., browser version or application name)

Step 2: Add Headers to Your cURL GET Request

To include headers in your cURL GET request, use the -H flag followed by the header key and value. If you need to add multiple headers, repeat the -H flag for each. Here’s a basic example:

curl -H "Accept: application/json" -H "Authorization: Bearer YOUR_ACCESS_TOKEN" https://api.example.com/data

This cURL GET request includes two headers: the Accept header, which specifies the content type the client accepts, and the Authorization header, which provides the access token required for authentication.

Step 3: Customize Your Request with Optional Flags

You may want to use optional flags to refine your cURL GET request further. Some useful flags include the following:

  1. -X: Specify the request method (e.g., GET, POST, PUT, DELETE)
  2. -I: Fetch only the headers of the response, not the actual content
  3. -L: Follow redirects automatically
  4. -o: Save the output to a file

Here’s an example of a cURL GET request with optional flags:

curl -X GET -I -H "Accept: application/json" -H "Authorization: Bearer YOUR_ACCESS_TOKEN" -L https://api.example.com/data -o output.txt

This request will fetch only the response’s headers, follow any redirects, and save the output to a file named “output.txt.”

Handling Response

By default, cURL will output the response body to the Terminal. However, you may want to save the response to a file or process it differently. To save the response to a file, you can use the -o option:

curl -o response.json https://example.com/api

This will save the response body to a file named response.json If you want to see the HTTP headers, you can add the -I  option to the command like this:

curl -I https://example.com/api/endpoint

  This command will return the HTTP headers for the response. If you want to save the response to a file, you can add the -o option like this:

curl -o response.json https://example.com/api/endpoint

  This command will save the response to a file named response.json. How to Measure API Response Time with cURL? Measuring API response time with cURL is a simple process. First, you must use the -w option to specify a custom output format. For example, to display the total time taken to receive the response, you can use the following command:

curl -w "Total time: %{time_total}\n" https://example.com/api/endpoint

This command will display the total time taken to receive the response. You can also use the -s  option to silence any progress or error messages like this:

curl -s -w "Total time: %{time_total}\n" https://example.com/api/endpoint

This command will display only the total time taken to receive the response.

Common cURL Errors

While cURL is useful, users may encounter errors during its operation.

Error 6: Could not resolve host

This error occurs when cURL cannot resolve the domain name in the provided URL. Common causes include typos in the URL or DNS resolution issues.

Error 7: Failed to connect

Error 7 indicates that cURL could not establish a connection to the target server. This could be due to server-side issues, network connectivity problems, or a misconfigured firewall.

Error 28: Operation timed out

When cURL cannot complete the request within the specified time, Error 28 occurs. This can result from slow network connections or an unresponsive server.

Error 35: SSL/TLS handshake failure

Error 35 signifies a failure during the SSL/TLS handshake process. It can be caused by various factors, such as outdated SSL libraries or incompatible SSL/TLS configurations between client and server.

Error 60: SSL certificate problem

This error indicates that cURL encountered an issue with the server’s SSL certificate, such as an expired certificate or an untrusted certificate authority.

Error 5: Transfer Closed with Outstanding Read Data Remaining

This error indicates that the connection was closed before all the expected data could be received, often due to server-side issues.

Troubleshooting cURL Errors

Step 1: Verify the URL

Ensure the URL you’re trying to access is correct, without any typos or invalid characters. Use the right protocol (HTTP or HTTPS) and double-check the domain name and path.

Step 2: Check Your Network Connection

Verify that your internet connection is working correctly and that you can access other websites. If you’re behind a proxy or firewall, ensure that it is configured correctly.

Step 3: Inspect SSL/TLS Certificates

Verify the server’s certificate using an online SSL checker or a browser for SSL certificate errors. If you’re using a self-signed certificate, ensure you’ve added it to your system’s trusted certificate store.

Step 4: Adjust Timeouts

If you’re encountering timeouts, try increasing the timeout value in your cURL request. Remember that setting it too high may cause the request to hang indefinitely if there are server-side issues.

Step 5: Update cURL and Dependencies

Ensure that you’re using the latest version of cURL and its dependencies. Updating these components can often resolve compatibility issues and improve performance.

Advanced Troubleshooting Techniques

Using Debugging Options

cURL provides several debugging options that can help you diagnose the root cause of errors. For instance, use the -v (verbose) flag to display detailed information about the request and response or the --trace option to generate a detailed trace of the entire operation.

Analyzing cURL Output

Inspect the output of your cURL command to identify issues with the request or response. Look for specific error messages, HTTP status codes, or other information to help you pinpoint the problem.

Monitoring Network Traffic

Use network monitoring tools like Wireshark or tcpdump to capture and analyze the network traffic between your machine and the target server. This can help you identify issues with your network or the server’s configuration.

Final Words

cURL errors can be challenging, but with the right approach and tools, you can quickly identify and resolve issues. I hope this Genius Geek guide will serve you well!

Gyula Virag
Gyula Virag

Gyula is a developer and a passionate geek father with a deep love of online marketing and technology. He always seeks challenging adventures and opportunities to create something permanent in the digital world.

Articles: 51