Linux Command Summary --curl command

Introduction to curl command

curl is a command-line tool, the role of a network request, and then extracted to obtain data showing the "standard output" (stdout) in the above.

(

linux There are three standard input and output, respectively STDIN, STDOUT, STDERR, the corresponding number is 0.

STDIN standard input, default information is read from the keyboard;

STDOUT is the standard output, the default output the result to the terminal;

STDERR is the standard error, the default result to the output terminal.

Since the default STDOUT and STDERR are displayed on the terminal, in order to distinguish, there is a defined number of 0, 1, represents STDOUT, STDERR represented by 1 2.

)

It supports multiple protocols, for example to explain how to use it for web development below.

In Linux curl is a file transfer tool use URL rules work at the command line, you can say that is a very powerful http command-line tool. It supports upload and download files, is integrated transmission tool, but traditionally, used to refer to url to download tools.

grammar

Syntax: # curl [option] [url]

Common parameters:

-A / - user-agent <string> user agent to a server provided

-b / - cookie <name = string / file> cookie string or file reading position 
-c / - after the cookie-jar <file> operation to write a cookie to the file 
-C / - continue- at <offset> Resume turn 
-D / - dump-header <file > writes the header information to the file 
-e / - referer URL 
-f / - fail when an error is not displayed http connection failed 
- o / - output is written to the output file 
-O / - remote-name is written to the output file, the remote file name retained 
-r / - range <range> retrieved from the HTTP / 1.1 or FTP server byte ranges 
-s / - silent silent mode. Does not output anything 
-T / - upload-file <file > upload files 
-u / - user <user [: password]> set the server's user and password
-w / - write-out [format ] What is the output after completion
-x / - proxy <host [: port]> HTTP proxy on a given port 
- # / - progress-bar progress bar shows the current state of the transfer

Examples

First, view the page source code

Directly after the curl command with the URL, you can see the page source. We www.sina.com URL, for example (select the Web site, mainly because of its short page code):

  $ curl www.sina.com

  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  <html><head>
  <title>301 Moved Permanently</title>
  </head><body>
  <h1>Moved Permanently</h1>
  <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
  </body></html>

If you want to survive this page, you can use `-o` parameter, which is equivalent to using wget commands.

  $ Curl -o [filename] www.sina.com

Second, automatically jump

Some URLs are automatically jump in. Use `-L` parameters, curl will jump to the new URL.

  $ curl -L www.sina.com

Type the above command, the result automatically jump to www.sina.com.cn.

Third, the display header information

`-I` parameters can be displayed in the header http response, along with the web page code.

  $ curl -i www.sina.com

  HTTP/1.0 301 Moved Permanently
  Date: Sat, 03 Sep 2011 23:44:10 GMT
  Server: Apache/2.0.54 (Unix)
  Location: http://www.sina.com.cn/
  Cache-Control: max-age=3600
  Expires: Sun, 04 Sep 2011 00:44:10 GMT
  Vary: Accept-Encoding
  Content-Length: 231
  Content-Type: text/html; charset=iso-8859-1
  Age: 3239
  X-Cache: HIT from sh201-9.sina.com.cn
  Connection: close

  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  <html><head>
  <title>301 Moved Permanently</title>
  </head><body>
  <h1>Moved Permanently</h1>
  <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
  </body></html>

`-I` parameter is displayed only the header information http response.

Fourth, the display communication process

`-V` parameter may be displayed to the entire process http communication, comprising port and http request header information.

  $ Curl -v www.sina.com

  * About to connect() to www.sina.com port 80 (#0)
  * Trying 61.172.201.195... connected
  * Connected to www.sina.com (61.172.201.195) port 80 (#0)
  > GET / HTTP/1.1
  > User-Agent: curl/7.21.3 (i686-pc-linux-gnu) libcurl/7.21.3 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18
  > Host: www.sina.com
  > Accept: */*
  > 
  * HTTP 1.0, assume close after body
  < HTTP/1.0 301 Moved Permanently
  < Date: Sun, 04 Sep 2011 00:42:39 GMT
  < Server: Apache/2.0.54 (Unix)
  < Location: http://www.sina.com.cn/
  < Cache-Control: max-age=3600
  < Expires: Sun, 04 Sep 2011 01:42:39 GMT
  < Vary: Accept-Encoding
  < Content-Length: 231
  < Content-Type: text/html; charset=iso-8859-1
  < X-Cache: MISS from sh201-19.sina.com.cn
  < Connection: close
  < 
  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  <html><head>
  <title>301 Moved Permanently</title>
  </head><body>
  <h1>Moved Permanently</h1>
  <p>The document has moved <a href="http://www.sina.com.cn/">here</a>.</p>
  </body></html>
  * Closing connection #0

If you think the above information is not enough, then the following command to view more detailed communication process.

  $ curl --trace output.txt www.sina.com

or

  $ curl --trace-ascii output.txt www.sina.com

After running, open output.txt file viewing.

Fifth, sending form information

Send form Offers GET and POST methods. GET method is relatively simple, as long as the data is appended to the URL line.

  $ curl example.com/form.cgi?data=xxx

POST method must separate the data and URL, curl necessary to use --data parameters.

  $ curl -X POST --data "data=xxx" example.com/form.cgi

If your data has not been encoded form, but also allows you to curl encoding parameter is `--data-urlencode`.

  $ curl -X POST--data-urlencode "date=April 1" example.com/form.cgi

Six, HTTP verb

curl default HTTP verb is GET, use `-X` parameters may support other verbs.

  $ curl -X POST www.example.com

  $ curl -X DELETE www.example.com

Seven, file upload

Assuming that the file upload form is this:

  <form method="POST" enctype='multipart/form-data' action="upload.cgi">
    <input type=file name=upload>
    <input type=submit name=press value="OK">
  </form>

So you can upload files with a curl:

  $ curl --form upload=@localfilename --form press=OK [URL]

Eight, Referer field

Sometimes you need to http request header information, a referer field represents a jump from where you came.

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

Nine, User Agent field

This field is used to indicate the device information of the client. Sometimes the server according to this field, for different devices, return to the page in different formats, such as mobile phones and desktop versions.

iPhone4 is the User Agent

  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 simulate this:

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

Ten, cookie

Use `--cookie` parameters help the curl send cookie.

  $ curl --cookie "name=xxx" www.example.com

As for the specific cookie value can be obtained from http response header information `Set-Cookie` field.

`-C cookie-file` can be saved to a file server returns a cookie,` -b cookie-file` can use this file as cookie information, for subsequent requests.

  $ curl -c cookies http://example.com
  $ curl -b cookies http://example.com

Eleven, increased header information

Sometimes necessary in the http request, add a header itself. `--Header` parameters can play this role.

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

Twelve, HTTP Authentication

Some domain HTTP authentication is required, then curl need to use `--user` parameters.

  $ curl --user name:password example.com

Guess you like

Origin www.cnblogs.com/hanjiali/p/11696647.html