Methods for using Curl to customize HTTP headers under Linux systems

Source: Internet
Author: User
Tags curl http request

Curl is a powerful command-line tool that can pass information to the server or get data from the server over the network. He supports a wide range of transport protocols, especially HTTP/HTTPS and others such as Ftp/ftps, RTSP, Pop3/pop3s, SCP, and imap/imaps protocols. When you use Curl to send an HTTP request to a URL, it uses an HTTP header that contains only the necessary header fields (such as: User-agent, Host, and Accept) by default.

In some cases, you might want to overwrite the default HTTP header in an HTTP request or add a new custom header field. For example, you might want to rewrite the "HOST" field to test a load balancing, or to impersonate a particular browser by overriding the "User-agent" string to address some of the access restrictions.

To address all of these issues, curl provides an easy way to fully control the HTTP headers of outgoing HTTP requests. The parameter you need is "-H" or "--header".

To define multiple HTTP header fields, the "-H" option can be specified more than once in the Curl command.

For example, the following command sets 3 HTTP header fields. That is, the "HOST" field has been overridden and two fields ("Accept-language" and "cookies") have been added

The code is as follows:

$ Curl-h ' host:157.166.226.25 '-h ' accept-language:es '-h ' cookie:id=1234 ' http://cnn.com

For standard HTTP header fields such as "User-agent", "Cookie", "Host", there is usually another way of setting. The Curl command provides specific options for setting these header fields:

-A (or--user-agent): Sets the User-agent field.

-B (or--cookie): Sets the "Cookie" field.

-E (or--referer): Sets the Referer field.

For example, the following two commands are equivalent. Both commands also change the "User-agent" string for the HTTP header.

The code is as follows:

$ curl-h "User-agent:my Browser" http://cnn.com

$ curl-a "My browser" http://cnn.com

Wget is another command-line tool similar to curl that you can use to get a URL. And wget also allows you to use a custom HTTP header.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.