Skip to main content
All CollectionsGetting StartedHow to: general
How to Send HTTP Headers Using cURL
How to Send HTTP Headers Using cURL
Alex MarsProxies avatar
Written by Alex MarsProxies
Updated over 7 months ago

When you browse the World Wide Web, your browser sends HTTP requests with HTTP headers holding specific information. HTTP header coveys the website client's information (user agent, operating system, language, etc.), caching instructions, content type, and more. An authorization header handles client-server authentication, so HTTP headers play an essential role in web scraping and computer networking.

Whenever you scrape a website, your client sends HTTP headers with yours, and information request details. Sometimes you may need to pass multiple headers or send a custom HTTP header to get a specific content type. That's when you turn to cURL (client URL) software to use cURL command lines to streamline data exchange.

What Is cURL?

Client for URL is an open-source software tool for online data exchange with web servers. It supports most Internet protocols, including HTTP(S), FTP, LDAP, SMB, TELNET, and more. cURL lets you send custom HTTP headers, and multiple headers simultaneously to streamline data extraction.

cURL is text-based and requires the user to follow strict URL syntax and command-line rules. You can use it to test APIs, scrape the web, identify client-server communication issues, or simply download files. But how to send headers with cURL to get the required content type?

Who Can Use cURL?

cURL is available on all new versions of the most popular operating systems. It is easily accessible on Windows 10 and later, macOS, and numerous Linux distributions. In Windows, simply open the command prompt and type curl --help.

curl1.png

It will display a list of essential cURL commands, like -i to include protocol response headers, -A to send a specific user agent to the server, and others. The list is not full, and you can use curl --help [category] to get a comprehensive list of each category commands or check out the documentation.

How to Send a cURL Request

You send a cURL HTTPS request when you require specific data from the server, often accompanied by additional instructions. For example, you can get data in JSON format using this command line:

curl -H "Accept: application/json" https://api.example.com/example

This particular HTTP header tells the server you require data in JSON format. The server then generates response headers to inform the client of the following data transfer rules. Remember that cURL deals only with data transferring, and you must specify the header name and other cURL HTTPS request details correctly.

Crafting Custom cURL Headers

The syntax of custom headers follows the same structure as a standard HTTP request.

You will start with the -H flag to craft a custom HTTP header to make it look like this:

curl -H "key-name: value" URL

Write down the desired HTTP header key before the colon, then insert specific header data in the value section. Your targeted URL follows after the quotes.

Sending Multiple Headers in One cURL Command

You can easily send multiple headers with cURL using one command. It is useful when communicating with APIs that require different header combinations in one request. Furthermore, it makes your cURL HTTPS commands tidy and easier to manage. Here's how to do it using the primary -H flag:

curl -H "header1: value1:" -H "header2: value2" <...> URL

The syntax is straightforward. You write as many header keys and values as required, each with a separate -H flag one after the other. After the last pair, you write down the target URL.

Managing Authenticated Requests With cURL

Firstly, you must know what authentication is required by the API or web server. The most basic is the username:password combination included in the header, but it's also the least secure. Instead, you can use an API key or a Bearer token-based authentication whenever available. Here's how to create an authorization header with cURL.

Basic authentication:

curl -u username:password URL

API Key authentication:

curl -H "X-API-Key: APIkeyexample" URL

Bearer token authentication:

curl -H "Authorization: Bearer Yourtokenexample" URL

On most occasions, you will have to obtain an API key or authentication token from the provider to initiate a secure data exchange.

Common Mistakes and Troubleshooting

Like with most command lines, many issues happen due to faulty spelling. Here are a few common mistakes everyone can make.

  • Syntax errors

Double-check that you have written the URL correctly with a specified protocol. Make sure you use the correct flag (they are case-sensitive) and HTTP header: value structure with a colon and required quote marks.

  • Handling redirects

cURL does not handle redirects, so you have to input the exact URL you are targeting. You can also use the -L flag to instruct cURL to follow redirects with this command:

curl -L https://www.yoururl.com/page

  • API authentication errors

You must use the authentication option accepted by the API or particular server. Usually, you can find the required authentication method within API documents or specific website instructions.

Conclusion

This covers the basics of HTTP request crafting using cURL. They will help you specify the required data format, utilize different user agents, exchange data via API, and more. Remember to double-check your syntax if you encounter errors, and use the cURL --help command to explore more options.

Did this answer your question?