Use curl to simulate requests for HTTP

In the process of client development, we always have to do api docking with the back end. Sometimes we need to debug the data format returned. Sometimes we need to run the client to send requests. This is inefficient. Here we introduce curl, a useful tool.

curl

curl is a tool to transmit data to the server. It supports http, https, ftp, ftps, scp, sftp, tftp, telnet and other protocols. Here, we will explain some common usage of HTTP only. Please search for the specific installation.

Open Baidu

curl http://www.baidu.com

Then you will see Baidu's page source code output.

If you want to save this page, you can do this:

curl http://www.baidu.com > /tmp/baidu.html

You will see a progress bar and the source code is redirected to / tmp/baidu.html.

Or:

curl -o /tmp/baidu.html http://www.baidu.com

GET request

By default, a direct request for a URL is a get request, and the parameters are spliced directly into the url, such as

curl http://www.baidu.com/s?wd=curl

Baidu initiates a query request at the above request meeting with the parameter wd=url

POST request

curl -d "name=test&page=1" http://www.baidu.com

- d. The parameter specifying form is executed as POST.

Show only Header

curl -I  http://www.baidu.com

You can see that only some header information is returned

HTTP/1.1 200 OK
Date: Fri, 07 Nov 2014 09:48:58 GMT
Content-Type: text/html; charset=utf-8
Connection: Keep-Alive
Vary: Accept-Encoding
Set-Cookie: BAIDUID=E9DB2F0AC95CB6BFDAD9D5CFDCED0A12:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BAIDUPSID=E9DB2F0AC95CB6BFDAD9D5CFDCED0A12; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BDSVRTM=0; path=/
Set-Cookie: BD_HOME=0; path=/
Set-Cookie: H_PS_PSSID=9725_9165_1465_7800_9452_9498_6504_9509_6018_9700_9757_9531_9478_7798_9453_9793_9024; path=/; domain=.baidu.com
P3P: CP=" OTI DSP COR IVA OUR IND COM "
Cache-Control: private
Cxy_all: baidu+3057b288b211c770a1463cc8519b62a8
Expires: Fri, 07 Nov 2014 09:48:17 GMT
X-Powered-By: HPHP
Server: BWS/1.1
BDPAGETYPE: 1
BDQID: 0xfa28eff900012706
BDUSERID: 0

Display communication process

- The v parameter can display the whole process of an http communication, including port connection and http request header information.

curl -v www.baidu.com

* Adding handle: conn: 0x7ffe4b003a00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7ffe4b003a00) send_pipe: 1, recv_pipe: 0
* About to connect() to www.baidu.com port 80 (#0)
*   Trying 61.135.169.125...
* Connected to www.baidu.com (61.135.169.125) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.30.0
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 07 Nov 2014 09:49:49 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: Keep-Alive
< Vary: Accept-Encoding
< Set-Cookie: BAIDUID=062E02D23FBB651CF8455B699DF02B64:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BAIDUPSID=062E02D23FBB651CF8455B699DF02B64; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BDSVRTM=0; path=/
< Set-Cookie: BD_HOME=0; path=/
< Set-Cookie: H_PS_PSSID=7744_1429_7801_9583_9499_6506_6018_9769_9699_9757_9532_9477_7799_9453_9716_9023; path=/; domain=.baidu.com
< P3P: CP=" OTI DSP COR IVA OUR IND COM "
< Cache-Control: private
< Cxy_all: baidu+7dcb6b3c03d32c334d42f311919a14d6
< Expires: Fri, 07 Nov 2014 09:49:20 GMT
< X-Powered-By: HPHP
* Server BWS/1.1 is not blacklisted
< Server: BWS/1.1
< BDPAGETYPE: 1
< BDQID: 0xadb706860000088f
< BDUSERID: 0

If you don't think the above information is enough, the following command allows you to view the communication process in more detail.

curl --trace output.txt www.baidu.com

perhaps

curl --trace-ascii output.txt www.baidu.com

After running, please open the output.txt file to see.

HTTP method

curl's default HTTP method is GET, which supports other verbs with the - X parameter.

curl -X POST www.example.com

curl -X DELETE www.example.com

Referer field

Sometimes you need to provide a referer field in the http request header to indicate where you jumped.

curl --referer http://www.example.com http://www.example.com

User Agent field

This field is used to represent the device information of the client. Servers sometimes return pages in different formats for different devices, such as mobile and desktop versions, according to this field.

The User Agent for the iPhone 4 is

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7

curl can be simulated as follows:

curl --user-agent "[User Agent]" [URL]

Increase header information

Sometimes you need to add a header to your http request. The header parameter can do this.

curl --header "Content-Type:application/json" http://example.com

Keywords: curl encoding Mobile ftp

Added by ffsja on Mon, 12 Aug 2019 13:16:41 +0300