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 ")
- $ 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.
- $ 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 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: