What does the -H option do in Curl?

The -H option in Curl is used to set custom HTTP headers to be included in the request made by Curl.

When you use the -H or --header option followed by a field: value pair, Curl will include that header in the request it sends to the server. This can be useful in a variety of situations, such as when you need to set authorization tokens, specify a user-agent, or provide other information that the server may require.

Here is an example of how to use the -H option with Curl:

curl -H "Content-Type: application/json" -H "Authorization: Bearer your_token" https://example.com/api/resource

In this example, two headers are being set: Content-Type and Authorization.

  • The Content-Type header tells the server that the request body format is JSON.
  • The Authorization header is providing a bearer token for authenticating the request.

You can include as many -H options as you need in a single Curl command to set multiple headers.

Note: The -H option must be followed by a space, then the header field and value in the format field: value. Do not include any spaces before or after the colon.

In Python, you can achieve the same with the requests library:

import requests

headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_token',
}

response = requests.get('https://example.com/api/resource', headers=headers)

In JavaScript, using the fetch API:

fetch('https://example.com/api/resource', {
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your_token',
  }
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));

Remember to replace 'Bearer your_token' with your actual bearer token and 'https://example.com/api/resource' with the URL you are trying to access.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon