Loading...

What is cURL and how to send cURL GET requests?

🔥 Quickfire # Magento # PHP
6 Mins
Jayram Prajapati  ·   25 Sep 2024
How to sent curl request
service-banner

Understanding HTTP requests and APIs is crucial for automating tasks and troubleshooting especially. When I need to send an HTTP request quickly, I always find using cURL.

cURL (Client URL) is a kind of technology that you can use to make a connection between a web server and your device by writing commands in the command line. By typing the server URL and the data you want to send it, cURL gives you various possibilities to perform different types of the requests, which are complementary to the tools like Postman and Insomnia which run from the terminal.

This is because in this article, I will be teaching what the cURL command is, how it works, and give some typical examples and use annotations.

What is cURL?

cURL (Client URL, pronounced "curl") is a software that allows you to quickly and directly exchange data between your device and the server over the command line through the terminal. In cURL, you state the server URL (problematic internet server address) and the data you want to send.

Postman and Insomnia have a user-friendly interface for making different requests to URLs, while cURL does the same thing directly right from the terminal. They can run on Linux, Mac, or Windows.

The primary cURL command is: curl http://example.com. In this example, it can return the HTML source code of the webpage as you're asking it to fetch the URL.

cURL makes use of the libcURL library, offering support for various transfer protocols including HTTPS, SMTP, and FTP. It also allows you to handle cookies, set proxies, and include authentication credentials when you are requesting data.

cURL can be used for many different purposes such as testing APIs, downloading data, checking websites, and following redirects, all of this is done from the command line.

How to Use Curl

Now just understand how you can use cURL in your system. Most operating systems already have curl installed. If you can’t find it simply go to (https://curl.se/download.html) and install. We also show you how to use cURL in Postman too.

To check if your system already has cURL installed, open your terminal 
and type curl --version. If it's installed, then you'll see the version
number and which features are enabled.

Running curl Examples in Terminal

Since most computers come with curl pre-installed, you can easily run a sample API request by copying code from our documentation and pasting it directly into your terminal.

Here’s an example of cURL get command:

curl -iX GET https://api._________.com/v1/labels?page_size=1 \
-H 'API-Key: __YOUR_API_KEY_HERE__'

This command sends a request to the API to retrieve a single label.

Note: If you're already connected to the API, the code samples will
automatically include your sandbox API key.

Running curl Examples in Postman

If you're not comfortable using curl in the terminal or just prefer graphical user interface (GUI) apps, Postman is a great option. You can even import curl commands into Postman. Just copy any curl command and paste them into Postman.

Step 1: Import curl

Open the Import dialog window by clicking the Import button on the Postman toolbar.

In the Import window, click the "Paste Raw Text" tab. This will open a large textbox where you can paste a curl command. After pasting the command, click the "Import" button at the bottom of the dialog window. Postman will automatically convert the curl command into a Postman request.

Step 2: Send the Request

Once the curl command is imported, a new Request tab will appear in the main Postman window. This Request will already include the URL, headers, query parameters, request body, and other details from the curl command. All you need to do is click the "Send" button. (You will need to enter your API key to get a result).

The response will show up on the right side or in the bottom half of the screen, depending on whether you have Postman set to a vertical or horizontal layout.

What is a GET request?

A GET request is a way for your browser to ask a server for information from a specific web page. For instance, when you enter a URL in your browser and hit enter, it sends a GET request to the server to fetch the HTML code for that page.

Other types of HTTP requests can also get HTML from a server, but they have different purposes. For example, a POST request is used to send data to a server, like when you log in with your username and password. Although you might see a web page after logging in, the POST request's main job was to send your information to the server.

In contrast, a GET request doesn’t send any data; it simply asks for a page or resources like images. Here’s a basic code of a cURL GET request:

curl http://elightwalk.com
This command will return the HTML that the web server sends back.

How to send GET requests with cURL

Now that we have the basics down, let’s look at how to send a GET request using cURL in the terminal. We’ll use https://elightwalk.com for our examples, as it's a straightforward HTTP request and response service. Here’s what we’ll cover:

  1. Simple cURL GET Requests
  2. Sending a GET Request with Parameters
  3. Retrieving GET HTTP Headers
  4. Receiving Responses in JSON Format
  5. Following Redirects
  6. Sending Cookies with a GET Request

These steps will help you effectively use cURL to send GET requests and handle various scenarios.

Step 1 - Simple cURL GET Requests

To send a basic GET request to the Elightwalk website, simply use the following command:

curl https://elightwalk.com/
  • curl: This is the command-line tool used for transferring data using various protocols (HTTP, FTP, etc.).
  • https://elightwalk.com/: This is the URL to which the GET request is being sent. In this case, it is the homepage of the Elightwalk website.
  • When executed, this command will fetch the HTML content of the specified webpage and display it in the terminal.

Step 2 - GET Request with Parameters

You can include parameters in your GET request. Here’s how to send additional data using -G and -d:

curl -G -d "param1=value1" -d "param2=value2" 
https://elightwalk.com/

Alternatively, you can append the parameters directly to the URL:

curl 'https://elightwalk.com/?param1=value1¶m2=value2'
  • -G: This option tells cURL to send the data as query parameters in the URL.
  • -d: This option allows you to specify the data you want to send. You can use it multiple times to add more parameters.
  • "param1=value1" and "param2=value2": These are key-value pairs representing the parameters you are sending. The values will be URL-encoded.

Step 3 - GET HTTP Headers

Use the -i option to get the HTTP headers together with the response body:

curl -i https://elightwalk.com/
  • -i: This option includes the HTTP response headers in the output. The headers contain metadata about the response, such as content type, content length, and server information.
  • If you want only the headers, use the --head option:
curl --head https://elightwalk.com/
  • --head: This command fetches only the headers of the response, which can be useful for checking server information or content type without downloading the entire webpage.

Step 4 - Retrieve Data in JSON Format

To request the response in JSON format, use the -H option:

curl -H "Accept: application/json" https://elightwalk.com/
  • -H: This option allows you to add custom headers to your request. Here, we're telling the server that we want to receive a response in JSON format.
  • "Accept: application/json": This header informs the server that the client (your cURL command) prefers a JSON response. The server must support returning JSON for this to work.

Step 5 - Follow Redirects

If the URL redirects to another location, use the -L option to follow the redirect:

curl -L https://elightwalk.com/
  • -L: This option tells cURL to follow any redirects returned by the server. For example, if the requested URL sends a 301 or 302 redirect, cURL will automatically follow that redirect to the new URL.

Step 6 - Send Cookies with a GET Request

To send cookies along with your GET request, use the -b option:

curl -b "username=John" https://elightwalk.com/
  • -b: This option allows you to include cookies in your request. Cookies can be used for session management or to provide user-specific data.
  • "username=John": This is a key-value pair representing the cookie you want to send. In this case, it sends a cookie named username with the value John.
  • These examples demonstrate how to effectively use cURL to send GET requests to the Elightwalk website in various scenarios.

Essence

We hope this guide has helped you understand the basics of cURL GET requests. Like any skill, practice is essential. We recommend spending some time sending requests to become comfortable with cURL.

If you're interested in learning how to make cURL requests in Magento 2, check out our blog post:

Also read: How to Make cURL Request in Magento 2

Stay connected and visit our blog page for more insights and updates!

Jayram Prajapati
Full Stack Developer

Jayram Prajapati brings expertise and innovation to every project he takes on. His collaborative communication style, coupled with a receptiveness to new ideas, consistently leads to successful project outcomes.

Most Visited Blog

How to Perform Simple CRUD with PHP and MySQL
Learn the fundamentals of using PHP and MySQL for CRUD operations. From single query execution to prepared statements, this guide will teach you everything you need to know to build dynamic web applications that interact with your database seamlessly. Whether you're a beginner or an experienced programmer, learn the fundamentals of developing dynamic web applications and efficiently manipulating database data.
How to make curl request in Magento 2? A Beginners Guide

Unlock the power of Curl requests in Magento 2 with our comprehensive guide. Learn how to make seamless API calls and optimize your Magento 2 development. Elevate your coding skills today!

How to create a module in Magento 2: Sample Module A Step-by-Step Guide

Embark on Magento 2 journey with confidence! Our step-by-step tutorial module development. Learn how to create a Module to harness the potential of Magento 2 customization for personalized and efficient e-commerce experience.