curl usage guide

Keywords: Linux Operation & Maintenance curl server

brief introduction

curl is a common command-line tool for requesting Web servers. Its name means the URL tool of the client.

Its function is very powerful, with dozens of command line parameters. If you are proficient, you can completely replace graphical interface tools such as Postman.

This paper introduces its main command line parameters as a daily reference for easy reference. The content is mainly translated from <curl cookbook> . In order to save space, the following examples do not include runtime output. Beginners can read what I wrote before curl beginner tutorial.

curl makes a GET request without any parameters.

$ curl https://www.example.com

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

-A

-The A parameter 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.com

The above command changes the user agent to Chrome browser.

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

The above command removes the user agent header.

You can also change the user agent by directly specifying the header through the - H parameter.

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

-b

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

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

The above command will generate a header Cookie: foo=bar, and send a cookie named Foo and value bar to the server.

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

The above command sends two cookies.

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

The above command reads the local file Cookie.txt, which is the Cookie set by the server (see - c parameter), and sends it to the server.

-c

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

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

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

-d

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

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

After using the - d parameter, the HTTP request will automatically add the header content type: application / x-www-form-urlencoded. And the request is automatically converted to the POST method, so - X POST can be omitted.

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

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

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

--data-urlencode

--The data URLEncode parameter is equivalent to - d, which is the data body of the POST request. The difference is that the sent data will be URL encoded automatically.

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

In the above code, there is a space between the sent data hello world, which needs URL coding.

-e

-The e parameter is used to set the HTTP header Referer, indicating the source of the request.

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

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

-The H parameter can achieve the same effect by directly adding a header Referer.

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

-F

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

$ curl -F 'file=@photo.png' https://google.com/profile

The above command will add the header content type: multipart / form data to the HTTP request, and then upload the file photo.png as a file field.

-The F parameter can specify the MIME type.

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

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

-The F parameter can also specify a file name.

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

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

-G

-The G parameter is 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, and the actual request URL is https://google.com/search?q=kitties&count=20 . If -- G is omitted, a POST request is issued.

If the data needs URL encoding, it can be combined with the -- data--urlencode parameter.

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

-H

-The H parameter adds the header of the HTTP request.

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

The above command adds the HTTP header accept language: en US.

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

The above command adds two HTTP headers.

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

The header of the HTTP request added by the above command is content type: application / JSON, and then send JSON data with the - d parameter.

-i

-i parameter prints the HTTP header of the server response.

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

After the above command receives the server response, first output the header of the server response, then leave a blank line, and then output the source code of the web page.

-I

-I parameter sends a HEAD request to the server, and the HTTP header returned by the server will be printed.

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

The above command outputs the response of the server to the HEAD request.

--The head parameter is equivalent to - I.

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

-k

-The k parameter specifies to skip SSL detection.

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

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 redirection. curl does not follow redirection by default.

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

--limit-rate

--Limit rate is used to limit the bandwidth of HTTP requests and responses and simulate a slow network speed environment.

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

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

-o

-The o parameter saves the server's response as a file, which is equivalent to the wget command.

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

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

-O

-The O parameter saves the server response to a file and takes the last part of the URL as the file name.

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

The above command saves the server response as a file named bar.html.

-s

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

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

Once an error occurs in the above command, the error message will not be displayed. If no error occurs, the operation results will be displayed normally.

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

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

-S

-The S parameter specifies that only error information is output, which is usually used with - o.

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

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/login

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

curl can identify the user name and password in the URL.

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

The above command can identify the user name and password in the URL and convert them into the HTTP header in the previous example.

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

The above command only sets the user name. After execution, curl will prompt the user to enter the password.

-v

-The whole process of v parameter output communication is used for debugging.

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

--The trace parameter can also be used for debugging, and the original binary data will be output.

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

-x

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

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

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

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

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

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.com

The above command is right https://www.example.com Issue a POST request.

 

Reference link

(end)

Posted by asmon on Mon, 25 Oct 2021 18:31:53 -0700