Skills of curl in linux

Get page content

> curl

Show HTTP headers

> curl -I
HTTP/1.1 200 OK
Server: openresty
Date: Fri, 04 Jun 2021 07:38:32 GMT
Content-Type: text/html
Content-Length: 12864
Last-Modified: Thu, 29 Apr 2021 01:39:01 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "608a0e35-3240"
Expires: Fri, 04 Jun 2021 08:38:32 GMT
Cache-Control: max-age=3600
Accept-Ranges: bytes

The HTTP header and file content are displayed at the same time. Use the - i option

> curl -i

Save link to file

We can use the > symbol to redirect the output to a local file.

> curl > index.html

You can also save the contents to a file through curl's - o/-O option.

  • -O (lowercase o): the result will be saved to the file name provided in the command line
  • -O (uppercase o): the file name in the URL will be used as the file name to save the output
> curl -o index.html
> curl -O

Note: when using the - O option, you must ensure that the file name is included at the end of the link, otherwise curl cannot save the file correctly. If there is no file name in the link, you should specify the file name manually with the - O option or use the redirection symbol

Download multiple files at the same time

We can use the - O or - O option to specify multiple links at the same time, and write the command in the following format:

> curl -O html   -O html
> curl -o p1.html  -o p2.html

Follow link redirection with - L

If we directly use curl to open some links that are reset backward, we can't get the web content we want in this case. For example:

> curl

301 Moved Permanently

301 Moved Permanently

When we open the link through the browser, we will automatically jump to . At this point, what we want curl to do is follow the jump of the link like a browser to get the final web content. We can add the - L option to the command to follow the link redirection:

> curl -L

Use - A to customize the user agent

We can use - A to customize the user * * *. For example, the following command will disguise as Android Firefox browser to request web pages:

> curl -A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0"

Use - H to customize the header

When we need to pass a specific header, we can follow the following command:

> curl -H "Referer:" -H "User-Agent: Custom-User-Agent"

Pass Cookie in header

> curl -H "Cookie: JSESSIONID=xxx"

Use - c to save cookies

When using cURL to access a page, cookies will not be saved by default

> curl -c "cookie.txt"

Use - b to read cookies

> curl -b "cookie.txt"

Send POST request with - d

There is a login page , you only need to submit your user name and password to log in. We can use cURL to complete this POST request, - d to specify the data to be sent, and - X to specify the way to send the data

> curl -d "userName=rumenz&passwd=123456" -X POST

When - d is used, if - X is omitted, the default mode is POST:

> curl -d "userName=rumenz&passwd=123456"

Read the data.txt text from the file

> curl -d "@data.txt"

Resume interrupted Download

> curl -C - -O

Download URL from file

If you use curl with xargs, you can download the file from the list of URL s in the file.

> xargs -n 1 curl -O < urls.txt

CURL set proxy

> curl  -U user:password  -x

If your proxy does not require authentication, you can skip - U user:password.

For example, Nginx configures the agent

server {
    listen                         3128;
    resolver             ;
    proxy_connect_allow            443 563;
    proxy_connect_connect_timeout  10s;
    proxy_connect_read_timeout     10s;
    proxy_connect_send_timeout     10s;
    location / {
        proxy_pass http://$host;
        proxy_set_header Host $host;

Upload files to ftp

> curl -u username:password -T jdk.tar.gz

Modify name resolution

> curl --resolve

yes Your query will tell curl to request the site from instead of using DNS or the / etc/hosts file.

Limit download rate

> curl --limit-rate 100K -O

HTTP authentication

Some domains require HTTP authentication, and curl requires the – user parameter.

> curl --user name:passwd

Original link:
WeChat official account: entry station

Tags: Linux

Posted on Fri, 22 Oct 2021 10:33:34 -0400 by jikishlove