This article was published on the wechat official account “The Beauty of Operation and Maintenance”, the official account ID: Hi-Linux.

“The beauty of operation and maintenance” is a feelings, attitude, focus on Linux operation and maintenance related technical articles to share the public number. The official account is committed to sharing various technical articles and publishing the most cutting-edge scientific and technological information for the majority of operation and maintenance workers. The core concept of the official account is to share. We believe that only sharing can make our community stronger. If you want to be the first to get the latest technical articles, please follow us!

Public account author Mike, a monthly salary of 3000 handyman. Engaged in IT related work for more than 15 years, keen on Internet technology field, identify with open source culture, have their own unique views on operation and maintenance related technology. I am willing to share my accumulated experience, experience and skills with you. Don’t miss this article. If you want to contact me, you can follow the public account for relevant information.

Introduction to the

Curl is a common command-line tool used to request a Web server. Its name simply stands for the CLIENT URL tool.

It is very powerful, with dozens of command-line parameters. If you are skilled, you can replace a graphical interface tool like Postman.

Using the instance

This article introduces its main command line parameters, as a daily reference, easy to consult. The content is mainly translated from curl Cookbook. In order to save space, the following examples do not include the runtime output, so beginners can check out my previous curl Tutorial.

If you do not take any parameters, curl will issue a GET request.

$ curl https://www.example.comCopy the code

The above command sends a GET request to www.example.com, and the content returned by the server is printed on the command line.

-A

-a Specifies the User Agent header of the client, that is, user-agent. The default user agent string for curl is 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.comCopy the code

The preceding command changes the user-agent browser to Chrome.

$ curl -A '' https://google.comCopy the code

The above command removes the user-agent header. You can also change the user-agent by specifying the header directly with the -h argument.

$ curl -H 'User-Agent: php/1.0' https://google.comCopy the code

-b

The -b parameter is used to send cookies to the server.

$ curl -b 'foo=bar' https://google.comCopy the code

The above command generates a header Cookie: foo=bar and sends a Cookie named foo with the value bar to the server.

$ curl -b 'foo1=bar' -b 'foo2=baz' https://google.comCopy the code

The above command sends two cookies.

$ curl -b cookies.txt https://www.google.comCopy the code

The above command reads the local file cookies. TXT, which contains the cookies set by the server (see the -c parameter), and sends it to the server.

-c

The -c parameter writes the cookies set by the server to a file.

$ curl -c cookies.txt https://www.google.comCopy the code

The above command writes the cookies set by the server’s HTTP response to the text file cookies.txt.

-d

The -d parameter is used to send the body of the POST request.

$curl -d'login=emma&password=123' -x POST https://google.com/login # $curl -d'login= Emma '-d' password=123' -x POST  https://google.com/loginCopy the code

When -d is used, HTTP requests will automatically add content-type: Application/X-www-form-urlencoded header. The request is automatically converted to the POST method, so you can omit -x POST.

The -d parameter can read data from a local text file and send it to the server.

$ curl -d '@data.txt' https://google.com/loginCopy the code

The above command reads the contents of the data.txt file and sends it to the server as the data body.

–data-urlencode

The –data-urlencode parameter is the same as -d, the body of data to send the POST request. The difference is that the sent data is automatically URl-encoded.

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

In the above code, there is a space between the hello World data sent, which needs to be URl-encoded.

-e

The -e parameter sets the HTTP header Referer, which indicates the source of the request.

$ curl -e 'https://google.com?q=example' https://www.example.comCopy the code

The above command sets the Referer header to https://google.com?q=example.

The -h argument can be achieved by adding the Referer header directly.

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

-F

The -f parameter is used to upload binary files to the server.

$ curl -F '[email protected]' https://google.com/profileCopy the code

The above command adds the content-type: multipart/form-data header to the HTTP request and uploads the file photo. PNG as the file field.

The -f parameter specifies the MIME type.

$ curl -F '[email protected]; type=image/png' https://google.com/profileCopy the code

Curl specifies the MIME type to be image/ PNG, otherwise curl will set the MIME type to application/octet-stream.

The -f parameter can also specify a file name.

$ curl -F '[email protected]; filename=me.png' https://google.com/profileCopy the code

In the above command, the original file is named photo.png, but the file received by the server is named me.png.

-G

The -g parameter is used to construct the query string for the URL.

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

The above command will send a GET request, actual requested URL to https://google.com/search?q=kitties&count=20. If –G is omitted, a POST request is issued.

If the data needs URL encoding, you can combine the –data–urlencode parameter.

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

-H

The -h parameter adds the HTTP request header.

$ curl -H 'Accept-Language: en-US' https://google.comCopy the code

The preceding command adds the HTTP header accept-language: en-us.

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

The above command adds two HTTP headers.

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

The above command adds a content-type: application/json header to the HTTP request, and then sends the JSON data with the -d parameter.

-i

The -i parameter prints the HTTP header that the server responds to.

$ curl -i https://www.example.comCopy the code

The above command received the server response, the first output server response header, and then empty line, and then output the source page.

-I

The -i parameter sends a HEAD request to the server, and the HTTP header returned by the server is printed.

$ curl -I https://www.example.comCopy the code

The above command outputs the server’s response to the HEAD request.

The –head argument is the same as -i.

$ curl --head https://www.example.comCopy the code

-k

The -k parameter skips THE SSL detection.

$ curl -k https://www.example.comCopy the code

The above command does not check whether the SSL certificate of the server is correct.

-L

The -l parameter causes the HTTP request to follow the server’s redirect. Curl does not follow redirects by default.

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

–limit-rate

–limit-rate is used to limit the bandwidth for HTTP requests and responses, simulating slow network speeds.

$ curl --limit-rate 200k https://google.comCopy the code

The above command limits the bandwidth to 200K bytes per second.

-o

The -o parameter saves the server response in a file, equivalent to the wget command.

$ curl -o example.html https://www.example.comCopy the code

The above command saves www.example.com as example.html.

-O

The -o parameter saves the server response to a file, using the last part of the URL as the file name.

$ curl -O https://www.example.com/foo/bar.htmlCopy the code

The above command saves the server response in a file called bar.html.

-s

The -s parameter does not output error and progress information.

$ curl -s https://www.example.comCopy the code

If an error occurs in the preceding command, no error message is displayed. If no errors occur, the results of the operation will be displayed normally.

If you want curl to produce no output, use the following command.

$ curl -s -o /dev/null https://google.comCopy the code

-S

The -s parameter specifies that only error information is displayed. It is usually used with -s.

$ curl -s -o /dev/null https://google.comCopy the code

The above command has no output unless an error occurs.

-u

The -u parameter is used to set the user name and password for server authentication.

$ curl -u 'bob:12345' https://google.com/loginCopy the code

The above command sets the user name to Bob and the password to 12345, and then converts them to the HTTP header Authorization: Basic Ym9iOjEyMzQ1.

Curl recognizes the user name and password in the URL.

$ curl https://bob:[email protected]/loginCopy the code

The above command recognizes the username and password in the URL and converts them to the HTTP header in the previous example.

$ curl -u 'bob' https://google.com/loginCopy the code

The preceding command only sets the user name, so curl prompts the user to enter the password.

-v

The -v parameter outputs the entire communication process, which is used for debugging.

$ curl -v https://www.example.comCopy the code

The –trace argument can also be used for debugging purposes and also outputs raw binary data.

$ curl --trace - https://www.example.comCopy the code

-x

The -x parameter specifies the proxy for the HTTP request.

$ curl -x socks5://james:[email protected]:8080 https://www.example.comCopy the code

The above command specifies that the HTTP request is made through the socks5 proxy of myproxy.com:8080.

If no proxy protocol is specified, the default is HTTP.

$ curl -x james:[email protected]:8080 https://www.example.comCopy the code

In the above command, the requested proxy uses the HTTP protocol.

-X

The -x parameter specifies the method of the HTTP request.

$ curl -X POST https://www.example.comCopy the code

The above command sends a POST request to https://www.example.com.

Refer to the link

  • Curl Cookbook

Source: Ruan Yifeng’s weblog

Original: http://t.cn/AiRQUQlz

Caption: from Google Image Search

Copyright: all rights reserved in this article

Contributions: Welcome contributions, email: [email protected]