How to Use cURL GET Requests

Scrapfly - Oct 4 - - Dev Community

How to Use cURL GET Requests

If you've ever wanted to extract data from websites, mastering cURL is an essential skill. Whether you're troubleshooting a server issue or performing web scraping, using cURL to send GET requests offers a powerful and flexible way to interact with APIs and web pages right from your terminal.

In this article, we'll dive into the fundamentals of curl GET requests, showing you how to handle URL structures, parameters, and headers for optimal results. Along the way, you'll learn practical examples and how to troubleshoot common issues with cURL.

Let's get started and unlock the potential of cURL for your web projects.

What is cURL?

cURL (short for "client URL") is an open-source command-line tool used to transfer data to or from a server. It supports numerous protocols such as HTTP, HTTPS, FTP, and more. When it comes to web scraping or making simple HTTP requests, cURL is a powerful, lightweight option.

You can use cURL to perform various types of requests, such as sending a GET request to retrieve data from a server. Moreover, it allows handling complex requests with the ability to add parameters, headers, and even authentication tokens.

Why Use cURL for Web Scraping?

cURL is particularly useful for web scraping because of its simplicity and flexibility. With cURL, you can interact with websites directly from your terminal, allowing you to:

  • Send HTTP requests (including cURL GET , POST , PUT , DELETE ).
  • Handle cURL with headers and cURL parameters for more refined control over requests.
  • Download files or save output.
  • Perform authentication, including cURL basic auth and proxy requests.

Furthermore, cURL supports HTTP/2 and can be integrated with tools like curl-impersonate, which allows your requests to better mimic browser-like behavior by adjusting the connection fingerprint. This helps bypass scraper blocking by mimicking browser-like TLS and HTTP2 configurations, avoiding detection from anti-scraping tools.

You can learn more about how Curl Impersonate Prevents Scraper Blocking in our dedicated article on using cURL for web scraping.

GET Requests Overview

Before we dive into the mechanics of curl GET requests, let's briefly cover HTTP methods. The most commonly used methods are:

  • GET: Used to request data from a server (e.g., download or view content) using a curl GET request.
  • POST: Sends data to a server (e.g., form submissions).
  • PUT: Updates or replaces data.
  • DELETE: Deletes data from a server.

In GET requests, cURL parameters are sent as part of the URL, making them perfect for scenarios where you need to retrieve data from a website or API.

Setting Up cURL

cURL is typically pre-installed on most operating systems, making it easy to get started with curl GET requests and other types of HTTP requests. However, it's always good practice to ensure you have the latest version installed. To check if you have cURL installed and verify the version, use this terminal command:



curl --version


Enter fullscreen mode Exit fullscreen mode

This command will display the current version of cURL installed on your system. If it's not installed, follow the instructions below to install cURL on different operating systems.

Linux

On most Linux distributions, you can install cURL by running the following terminal command:



sudo apt install curl


Enter fullscreen mode Exit fullscreen mode

This will install cURL and allow you to send HTTP requests, such as a GET request with cURL, directly from the terminal.

MacOS

If you're using macOS, the easiest way to install cURL is through the Homebrew package manager. Run this command in your terminal:



brew install curl


Enter fullscreen mode Exit fullscreen mode

Once installed, you can start using cURL to send GET requests, work with cURL headers, and handle cURL parameters for more advanced use cases.

Windows

For Windows users, cURL can be downloaded from the official cURL website, or you can use the Chocolatey package manager. Using Chocolatey, install cURL with this command:



choco install curl


Enter fullscreen mode Exit fullscreen mode

After installation, you can open your command prompt or PowerShell and start making cURL requests, including the curl GET command, without needing a separate GUI tool.

Even if you have curl installed Keeping cURL updated ensures that you're able to use the latest features, such as enhanced support for cURL with headers, cURL multiple headers, or handling new protocols and security standards. This is particularly important when working with curl GET requests for APIs or web scraping, where newer versions may offer better compatibility and performance.

Once cURL is installed and ready, you can start using it to interact with APIs or websites, including sending GET requests, which is one of the most common operations with cURL. Let's now take a look at how to use cURL to send GET requests.

Sending a GET Request with cURL

The curl GET command is simple and widely used to retrieve data from a server. The basic syntax for sending a GET request with cURL is:



curl [URL]


Enter fullscreen mode Exit fullscreen mode

For example, to perform a curl GET request from a simple API endpoint like httpbin.dev/get, you can use:



curl https://httpbin.dev/get


Enter fullscreen mode Exit fullscreen mode

The response from this API will look something like:



{
  "args": {},
  "headers": {
    "Host": "httpbin.dev",
    "User-Agent": "curl/7.68.0",
    "Accept": "*/*"
  },
  "url": "https://httpbin.dev/get"
}


Enter fullscreen mode Exit fullscreen mode

This cURL example shows the data returned by the server, including the headers and the URL used.

Adding URL Parameters

When sending a GET request with cURL, you often need to pass extra data to the server, such as search terms, filters, or pagination. These are called URL parameters, which are added to the request as part of the URL in the form of query strings.

The query string is a set of key-value pair parameters after the ? character in a URL. The structure for adding cURL parameters looks like this:



curl https://httpbin.dev/get?param1=value1&param2=value2


Enter fullscreen mode Exit fullscreen mode

In the example above, param1=value1 and param2=value2 are query strings, separated by an & symbol and preceded by a ? in the URL. Query strings are essential when you need to send specific data to a cURL API endpoint, allowing the server to interpret and return the relevant response based on the parameters you provide.

Use Case: Pagination and Filtering

In real-world scenarios, you can use curl with an API to retrieve paginated data, you might need to send curl GET request with parameters for pagination or filters. Here's curl GET example of adding pagination parameters to curl GET request example:



curl "https://web-scraping.dev/products?page=2&order=desc"


Enter fullscreen mode Exit fullscreen mode

This request uses page=2 parameter to indcate the 2nd results page and order=desc to indicate result ordering.

Next, let's take a look at value encoding which is often required for correct GET requests.

Handling URL Encoding

When your URL parameter values contain non-url safe characters, you'll need to encode them into a URL safe format - this is called URL encoding.

If you don't properly encode special characters, your request may fail or return incorrect results. This is because special characters (such as &, =, ?, and spaces) are reserved in URLs and are used for specific purposes like separating query string parameters. For instance, a space would be misinterpreted as a separator between parameters if not encoded as %20:



curl "https://httpbin.dev/get?query=hello%20world"


Enter fullscreen mode Exit fullscreen mode

In the above command, %20 represents a space character.

How to Perform URL Encoding

To avoid issues with special characters, always ensure your URL is encoded. Most programming languages, tools, and services offer built-in URL encoding functions. cURL does not automatically handle encoding, so you must encode any special characters manually. Here's a Python example:



from urllib.parse import quote

quote("me&myself and I?")
'me%26myself%20and%20I%3F'


Enter fullscreen mode Exit fullscreen mode

Automated URL Encoding with Scrapfly

Scrapfly offers a reliable solution to handle URL encoding and ensure your requests are correctly formatted, especially when dealing with web scraping tasks. You can use Scrapfly's encoding tools to correctly URL encode in your browser.

Adding Headers to GET Requests

When making HTTP requests, you may need to add custom cURL headers to provide additional information to the server.

Headers are key-value pairs that provide request meta dateails , such as:

  • Who's making the request? User-Agent
  • The expected result format? Accept
  • Authentication credentials? Authorization
  • Many more.

In a curl GET request, headers can be vital for successful requests and missing values or misconfiguration can result in failed request or even client blocking.

To set headers in cURL, use the -H option. For example, to include multiple headers in your request, such as a User-Agent and Accept-Language, the command would look like this:



curl -H "User-Agent: Mozilla/5.0" -H "Accept-Language: en-US" https://httpbin.dev/get


Enter fullscreen mode Exit fullscreen mode

This sends both custom headers with your request. If you want to verify that the headers were received, you can show response headers by including the -i option. The response might look like this:



{
  "headers": {
    "User-Agent": "Mozilla/5.0",
    "Accept-Language": "en-US",
    ...
  }
}


Enter fullscreen mode Exit fullscreen mode

If you're working with an API or server that requires a specific Host header, you can add it with the same -H option:



curl -H "Host: api.example.com" https://api.example.com/data


Enter fullscreen mode Exit fullscreen mode

This ability to send multiple headers with cURL allows you to tailor your request to the server's requirements and handle specific use cases like authentication or setting the expected response format.

Curl Show Headers

When making a curl GET request, it's often useful to see not just the body of the response but also the headers returned by the server. Headers provide key details such as the HTTP status code, content type, and caching rules. To show response headers in your curl request, use the -i option:



$ curl -i https://httpbin.dev/get
HTTP/2 200 
access-control-allow-credentials: true
access-control-allow-origin: *
content-security-policy: frame-ancestors 'self' *.httpbin.dev; font-src 'self' *.httpbin.dev; default-src 'self' *.httpbin.dev; img-src 'self' *.httpbin.dev https://cdn.scrapfly.io; media-src 'self' *.httpbin.dev; script-src 'self' 'unsafe-inline' 'unsafe-eval' *.httpbin.dev; style-src 'self' 'unsafe-inline' *.httpbin.dev https://unpkg.com; frame-src 'self' *.httpbin.dev; worker-src 'self' *.httpbin.dev; connect-src 'self' *.httpbin.dev
content-type: application/json; encoding=utf-8
date: Thu, 03 Oct 2024 08:06:24 GMT
permissions-policy: fullscreen=(self), autoplay=*, geolocation=(), camera=()
referrer-policy: strict-origin-when-cross-origin
strict-transport-security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
x-xss-protection: 1; mode=block
content-length: 589
...


Enter fullscreen mode Exit fullscreen mode

This command will display both the response headers and the body, giving you critical insights into how the server processed your request. For example, you might want to inspect the curl GET status code to ensure the request was successful or check what content type the server is sending. This is especially useful when troubleshooting and debugging your curl get requests.

Saving Results to a File

To save the output of your curl GET request to a file, you can use the -o or --output flag:



curl -o output.txt https://httpbin.dev/get


Enter fullscreen mode Exit fullscreen mode

This is particularly helpful when working with larger API responses or files that need to be reviewed later. For example, after using curl to request data from an API, you might want to save the result for further analysis or logging.

Proxy and Network Issues

If you are in a restricted network or need to route your curl GET request through a proxy, you can configure cURL to use a proxy server. This can be done using the -x option:



curl -x http://proxy.example.com:8080 https://httpbin.dev/get


Enter fullscreen mode Exit fullscreen mode

This ensures that your requests are correctly routed through the desired network proxy. For more advanced setups and cURL examples involving proxies, you can consult the official documentation.

Troubleshooting Common Issues with curl GET Requests

When using cURL, you may encounter several common issues that can impact the success of your GET request. Let's explore how to resolve some of these, from authentication problems to handling multiple headers.

Authentication Issues

Many APIs require authentication, and cURL supports different methods to handle these requirements. Whether you are using Basic Authentication or a token-based system, you can easily pass credentials with cURL.

Basic Authentication

For APIs using basic authentication, you can use curl with username and password authentication through the -u option followed by the username and password:



curl -u username:password https://httpbin.dev/basic-auth/username/password


Enter fullscreen mode Exit fullscreen mode

This sends the credentials in the request header to authenticate your session.

Token Authentication

For APIs using token-based authentication, you can set the header to include the bearer token. This is an example of a curl GET request with headers for token authentication:



curl -H "Authorization: Bearer your_token_here" https://api.example.com/data


Enter fullscreen mode Exit fullscreen mode

These cURL examples allow you to easily authenticate requests when accessing protected resources.

Handling Multiple Headers

When working with APIs, you might need to send multiple headers in your curl GET request. Headers such as User-Agent, Authorization, and Host are essential in many scenarios. You can use the -H option to set headers in cURL, adding as many as necessary for your request:



curl -H "User-Agent: Custom" -H "Authorization: Bearer your_token_here" -H "Host: api.example.com" https://api.example.com/data


Enter fullscreen mode Exit fullscreen mode

In this example, we are setting a User-Agent, passing an authorization token, and specifying a curl host header. These headers give you flexibility when interacting with various APIs or services, enabling you to meet the server's requirements for proper authentication and data handling.

Common cURL Errors

If you encounter issues like "Connection timed out" or "SSL certificate problems," cURL's verbose mode can help identify the cause. Using the -v option will provide a detailed breakdown of the request and response:



curl -v https://httpbin.dev/get


Enter fullscreen mode Exit fullscreen mode

Verbose mode helps you trace every step of the request, from DNS resolution to the server's response, including the curl GET status code and headers. This makes it easier to troubleshoot issues with SSL, timeouts, or misconfigurations.

For example, you might want to inspect the curl request get status to verify if the issue lies with the server response (e.g., a 404 error) or if there are issues with the request itself. Verbose output is especially useful when debugging curl GET request examples that aren't behaving as expected.

cURL Alternatives

While cURL is powerful, it can be a bit tricky to use for more complex requests. Here are some popular alternatives:

  • Curlie: A more user-friendly cURL alternative with an easier-to-read syntax. Learn more about Curlie here.
  • HTTPie: Another CLI tool designed to simplify HTTP requests and provide a more readable output.
  • Postman: A GUI-based HTTP client, perfect for testing and developing web APIs.

Scrapfly API – An Enhanced Solution for Web Scraping

For those seeking a more advanced and scalable solution for web scraping, Scrapfly offers a powerful API designed to handle the complexities of scraping at scale.

ScrapFly provides web scraping, screenshot, and extraction APIs for data collection at scale.

Here's a quick example of how you can integrate Scrapfly into your scraping workflow using cURL:



curl -G \
--request "GET" \
--url "https://api.scrapfly.io/scrape" \
--data-urlencode "proxy_pool=public_residential_pool" \
--data-urlencode "country=us" \
--data-urlencode "asp=true" \
--data-urlencode "render_js=true" \
--data-urlencode "Your ScrapFly API key" \
--data-urlencode "url=https://web-scraping.dev/products" \
--data-urlencode "headers[Referer]=https://www.google.com/"


Enter fullscreen mode Exit fullscreen mode

This example demonstrates how to send a POST request to Scrapfly's API to scrape a webpage, with options to set custom headers and enable JavaScript rendering, a feature often required for dynamic content extraction.

Try for FREE!

More on Scrapfly

FAQ

To wrap up our guide on using cURL for GET requests, let's address some common questions:

Can I Use cURL for Web Scraping?

Yes, cURL can be used for basic web scraping tasks. However, for more complex scraping, such as handling dynamic content, it's better to use tools like Scrapfly or integrate cURL with programming languages like Python.

How Can I Send Multiple Headers in a Single cURL Request?

To send multiple headers in a single cURL request, you can use the -H option for each header you want to add. This is useful when you need to set custom User-Agent, Authorization, or other headers simultaneously.

Example:



curl -H "User-Agent: CustomUserAgent" -H "Authorization: Bearer your_token" https://example.com


Enter fullscreen mode Exit fullscreen mode

This sends both the User-Agent and Authorization headers in the same request.

What's the Difference Between cURL and Curlie?

Curlie is a more user-friendly interface for cURL, offering easier syntax and colorful output formatting. It combines the flexibility of cURL with the simplicity of HTTPie.

Summary

In this guide, we explored how to perform a GET request using cURL. We covered the basics, such as sending GET requests, handling URL parameters, adding headers, troubleshooting common issues, and alternatives to cURL.

For more advanced web scraping, consider using Scrapfly, which provides a scalable and robust solution with enhanced features like proxy rotation and anti-bot bypass.

Feel free to check out our other cURL-related articles for more detailed tutorials!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player