Getting started with Linux: Setting custom HTTP headers in curl

Source: Internet
Author: User

Getting started with Linux: Setting custom HTTP headers in curl

Problem: I am trying to use the curl command to get a URL, but I also want to set some custom header fields in the outgoing HTTP request. How can I use custom HTTP headers in curl?

Curl is a powerful command line tool that can transmit information to the server or obtain data from the server over the network. It supports many transmission protocols, especially HTTP/HTTPS and other protocols such as FTP/FTPS, RTSP, POP3/POP3S, SCP, IMAP/IMAPS. When you use curl to send an HTTP request to a URL, it uses an HTTP header containing only necessary header fields (such as User-Agent, Host, and Accept) by default.

In some cases, you may want to overwrite the default HTTP header in an HTTP request or add a new custom header field. For example, you may want to rewrite the "HOST" field to test a Server Load balancer, or rewrite the "User-Agent" string to impersonate a specific browser to solve some access restrictions.

To solve all these problems, curl provides a simple method to completely control the HTTP header for outgoing HTTP requests. The parameter you need is "-H" or "-- header ".

To define multiple HTTP header fields, the "-H" option can be specified multiple times in the curl command.

For example, the following command sets three HTTP header fields. In other words, the "HOST" field is overwritten and two fields ("Accept-Language" and "Cookie") are added ")

  1. $ Curl-H 'host: 157.166.226.25 '-H' Accept-Language: els'-H' Cookie: ID = 100' http://cnn.com

For standard HTTP header fields such as "User-Agent", "Cookie", and "Host", there is usually another setting method. The curl command provides specific options to set these header fields:

  • -A (or -- user-agent): Set the "User-Agent" field.
  • -B (or -- cookie): Set the "Cookie" field.
  • -E (or -- referer): Set the "Referer" field.

For example, the following two commands are equivalent. Both commands have also changed the "User-Agent" string in the HTTP header.

  1. $ Curl-H "User-Agent: my browser" http://cnn.com
  2. $ Curl-A "my browser" http://cnn.com

Wget is another command line tool similar to curl that can be used to obtain a URL. In addition, wget allows you to use a custom HTTP header. Click here to view the details of the wget command.

Ubuntu users install the download tool cURL 7.36.0

Linux curl

Sharing of Curl usage and common functions in Unix

Curl command

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.