A guide to curl


A guide to curl

Reading guide

Curl is a common command-line tool used to request web servers. Its name means the URL tool of the client.

Its function is very powerful, and there are 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, easy to consult. The content is mainly translated from curl cookbook. In order to save space, the following examples do not include runtime output. Beginners can read the curl tutorial I wrote before.

With no parameters, curl is a get request.

$ curl https://www.example.com

The order is to www.example.com Send a get request, and the content returned by the server will be output on the command line.

  • -The a parameter specifies the user agent header of the client, that is, the 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 specify the header directly through the – h parameter to change the user agent.

$ curl -H 'User-Agent: php/1.0' https://google.com
  • -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 with a value of bar to the server.

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

The above command sends two cookies.

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

The above command reads the local file cookies.txt , which is the cookie set by the server (see – C parameter) and sent to the server.

  • -The 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 a text file cookies.txt 。

  • -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
$ 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 a 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

Read the above command data.txt The content of the file is sent to the server as a data body.

  • –data-urlencode

–The data URLEncode parameter is equivalent to – D, which is the data body to send 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 requires URL encoding.

  • -The e parameter is used to set the HTTP header referer, which indicates 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 。

  • -H parameter can achieve the same effect by directly adding header referer.
curl -H 'Referer: https://google.com?q=example' https://www.example.com
  • -The f parameter is used to upload binary files to the server.
$ curl -F '[email protected]' https://google.com/profile

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

  • -The f parameter can specify the MIME type.
$ curl -F '[email protected];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 '[email protected];filename=me.png' https://google.com/profile

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

  • -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. The actual URL of the request is https://google.com/search?q=k… 。 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
  • -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 above command adds the HTTP request header as content type: application / JSON, and then sends the JSON data with the – D parameter.

  • -The I parameter prints the HTTP header of the server response.
$ curl -i https://www.example.com

After receiving the response from the server, the above command first outputs the header of the response from the server, and then outputs the source code of the web page with a blank line.

  • -I parameter sends a head request to the server, and then prints out the HTTP header returned by the server.
$ curl -I https://www.example.com

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

  • –The head parameter is equivalent to – I.
$ curl --head https://www.example.com
  • -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.

  • -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 is used to limit the bandwidth of HTTP requests and responses, and to simulate a slow network environment.
$ curl --limit-rate 200k https://google.com

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

  • -O parameter saves the server’s response as a file, which is equivalent to WGet command.
$ curl -o example.html https://www.example.com

The above command will www.example.com Baocuncheng example.html 。

  • -The o parameter saves the server response as 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 to a file named bar.html 。

  • -The S parameter will not output error and progress information.
$ curl -s https://www.example.com

In case of an error in the above command, no error message will be displayed. If there is no error, the running result 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
  • -The S parameter specifies that only error messages are output and is usually used with – O.
$ curl -s -o /dev/null https://google.com

There is no output from the above command unless an error occurs.

  • -The U parameter is used to set the user name and password of the server authentication.
$ curl -u 'bob:12345' https://google.com/login

The above command sets the user name to Bob and password to 12345, and then converts it to HTTP header authorization: basic ym9iojmzq1.

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

$ curl https://bob:[email protected]/login

The above command can identify the user name and password in the URL and convert it to 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 parameter output communication of the whole process, 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
  • -The X parameter specifies the proxy for the HTTP request.
$ curl -x socks5://james:[email protected]:8080 https://www.example.com

The above command specifies that the HTTP request passes myproxy.com:8080 The Socks5 proxy of.

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

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

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

  • -The X parameter specifies the method of the HTTP request.
$ curl -X POST https://www.example.com

That’s right https://www.example.com Make a post request.

A kind of Author: Ruan Yifeng
Original text: http://www.ruanyifeng.com/blo…

A guide to curl

Recommended Today

Rust programming video tutorial (Advanced) – 024_ 3 syntax of all modes 3

Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content 1. Ignore values in mode(1) Use_ Ignore entire valueexample: fn foo(_: i32, y: i32) { println!(“This code only uses the y parameter: {}”, y); } fn main() { foo(3, 4); } Note: placeholders are used for parameters in the function, mainly when implementing […]