curl usage guidelines

Brief introduction

curl is commonly used command-line tool, used to request the Web server. Its name is the meaning of the client (client) of the URL tool.

It's very powerful, as many as dozens of command-line parameters. If skilled then Postman can replace this type of graphical interface.

This article describes its main command-line parameters, as a daily reference for easy access. Mainly translated from "curl Cookbook" . To save space, the following examples do not include output runtime, beginners can look at my previous write "curl tutorial for beginners" .

When no parameters, curl is to issue a GET request.


$ curl https://www.example.com 

The above command to www.example.comissue a GET request, the content server will be returned in the command line output.

-A

-AParameter specifies the client user agent header, i.e. User-Agent. curl default user agent string curl/[version].


$ curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com 

The above command will User-Agentchange the Chrome browser.


$ curl -A '' https://google.com 

The above command will remove User-Agentheader.

You can also -Hchange the parameters directly specify header User-Agent.


$ curl -H 'User-Agent: php/1.0' https://google.com 

-b

-bCookie parameters used to send to the server.


$ curl -b 'foo=bar' https://google.com 

The above command generates a header Cookie: foo=bar, transmitting to the server a name foo, a value barof Cookie.


$ curl -b 'foo1=bar' -b 'foo2=baz' https://google.com 

The above command transmits two Cookie.


$ curl -b cookies.txt https://www.google.com 

The above command reads local files cookies.txt, which are cookies (see server set -cparameters), and sends it to the server.

-c

-cCookie parameter settings to a file server.


$ curl -c cookies.txt https://www.google.com 

The above command will respond to HTTP server set Cookie written text file cookies.txt.

-d

-dParameters used to transmit data POST request body.


$ curl -d'login=emma&password=123'-X POST https://google.com/login # 或者 $ curl -d 'login=emma' -d 'password=123' -X POST https://google.com/login 

Used -dafter parameters, HTTP request header is automatically added Content-Type : application/x-www-form-urlencoded. And automatically converted POST request methods, it can be omitted -X POST.

-dLocal parameter data can be read text file is transmitted to the server.


$ curl -d '@data.txt' https://google.com/login 

The above command to read data.txtthe contents of a file, sent to the server as the data thereof.

--data-urlencode

--data-urlencodeEquivalent parameters -d, POST request body of data transmission, except that the data will automatically be transmitted URL encoding.


$ curl --data-urlencode 'comment=hello world' https://google.com/login 

In the above code, the data transmitted hello worldhas a space between, URL encoding is required.

-e

-eParameter sets the HTTP header Refererindicating the source of the request.


curl -e 'https://google.com?q=example' https://www.example.com 

The above command Refererheader is set https://google.com?q=example.

-HParameters by directly adding headers Refererto achieve the same effect.


curl -H 'Referer: https://google.com?q=example' https://www.example.com 

-F

-FParameters used to upload binary files to the server.


$ curl -F '' https://google.com/profile 

The above command will add HTTP request header Content-Type: multipart/form-data, then the file photo.pngas a filefield to upload.

-FParameters can be specified MIME type.


$ curl -F ';type=image/png' https://google.com/profile 

The above command specifies the MIME type image/png, otherwise it will curl MIME type is set application/octet-stream.

-FParameters can also specify the file name.


$ curl -F ';filename=me.png' https://google.com/profile 

The above command, the original file is named photo.png, but the server receives a file named me.png.

-G

-GParameters used to construct the query string of the URL.


$ curl -G -d 'q=kitties' -d 'count=20' https://google.com/search 

The above command will issue a GET request, URL is actually requested https://google.com/search?q=kitties&count=20. If omitted --G, it will issue a POST request.

If the URL-encoded data needs to be combined with --data--urlencodeparameters.


$ curl -G --data-urlencode 'comment=hello world' https://www.example.com 

-H

-HParameters are added to the HTTP request header.


$ curl -H 'Accept-Language: en-US' https://google.com 

The above command adds HTTP headers Accept-Language: en-US.


$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com 

The above two commands add HTTP headers.


$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login 

The above command adds HTTP request headers are Content-Type: application/jsonfollowed by -dthe transmission data parameters JSON.

-i

-iPrint out parameter server response HTTP headers.


$ curl -i https://www.example.com 

After the above command server response is received, the first output of the server response headers, then a blank line, then the output source web.

-I

-IHEAD request parameter sent, then the server will return an HTTP header to the print server.


$ curl -I https://www.example.com 

The output of the above command server response to a HEAD request.

--headEquivalent to parameters -I.


$ curl --head https://www.example.com 

-k

-kSSL parameter specifies the skipped detection.


$ curl -k https://www.example.com 

The above command does not check the server's SSL certificate is correct.

-L

-LParameters will follow HTTP server redirects requests. curl default does not follow redirects.


$ curl -L -d 'tweet=hi' https://api.twitter.com/tweet 

--limit-rate

--limit-rateUsed to limit the bandwidth of HTTP requests and responses to simulate slow speed environment.


$ curl --limit-rate 200k https://google.com 

The above command will limit the bandwidth of 200K bytes per second.

-O

-oParameters will be saved into a file server to respond, equivalent to the wgetcommand.


$ curl -o example.html https://www.example.com 

The above command will www.example.comsave as example.html.

-O

-OThe server responds with parameters saved to a file, and the last part of the URL as the file name.


$ curl -O https://www.example.com/foo/bar.html 

The above command will save the server to respond to a file named bar.html.

-s

-sParameter will not output errors and progress information.


$ curl -s https://www.example.com 

Once the above command error occurs, no error message is displayed. Error does not occur, it will display properly operating results.

If you want curl does not produce any output, you can use the following command.


$ curl -s -o /dev/null https://google.com 

-S

-SParameter specifies that only output error messages, and usually -sused together.


$ curl -s -o /dev/null https://google.com 

The above command no output, unless an error occurs.

-u

-uParameters used to set the server authentication username and password.


$ curl -u 'bob:12345' https://google.com/login 

The above command sets the user name bob, password 12345, and then into the HTTP header Authorization: Basic Ym9iOjEyMzQ1.

curl inside the URL can identify a user name and password.


$ curl https://bob:12345@google.com/login 

The above command can identify which URL of a username and password, which was converted to the inside of example HTTP header.


$ curl -u 'bob' https://google.com/login 

The above command only set a user name, after the execution, curl will prompt the user to enter a password.

-v

-vOutput of the entire communication process parameters for debugging.


$ curl -v https://www.example.com 

--traceParameters can also be used for debugging, but also outputs raw binary data.


$ curl --trace - https://www.example.com 

-x

-xParameter specifies the proxy HTTP requests.


$ curl -x socks5://james:.com:8080 https://www.example.com 

HTTP request specified by the above command myproxy.com:8080is issued socks5 proxy.

If you do not specify the agency agreement, the default is HTTP.


$ curl -x james:.com:8080 https://www.example.com 

The above command, the agent requests using the HTTP protocol.

-X

-XThe method of HTTP request specifies parameters.


$ curl -X POST https://www.example.com 

The above command https://www.example.comrequests issued POST.

Reference links

Guess you like

Origin www.cnblogs.com/xzlive/p/11493144.html