10 common usage Examples of command-line curl under Linux

Source: Internet
Author: User
Tags ranges

In Linux, Curl is a file transfer tool that works under the command line using URL rules, which can be said to be a powerful HTTP command-line tool. It supports file upload and download, is a comprehensive transfer tool, but by tradition, the custom called URL for download tool.

Grammar:# curl [option] [url]

1. Get page Content

When we use curl without any option, a GET request is sent by default to get the link content to standard output.

Curl http://www.baidu.com

2. Display HTTP headers

If we only want to display the HTTP headers without displaying the file contents, you can use the-I option:

Curl-i http://www.baidu.com

The output is:

http/1.1 Okserver:bfe/1.0.8.18date:sat, APR 2018 02:31:12 Gmtcontent-type:text/htmlcontent-length:277last-modi Fied:mon, June 02:50:08 Gmtconnection:keep-aliveetag: "575e1f60-115" Cache-control:private, No-cache, No-store, p Roxy-revalidate, No-transformpragma:no-cacheaccept-ranges:bytes

  

You can also display both the HTTP header and the file contents, using the-I option:

Curl-i http://www.baidu.com

The output is:

http/1.1 okaccept-ranges:bytescache-control:private, No-cache, No-store, Proxy-revalidate, No-transformconnection:keep-alivecontent-length:2443content-type:text/htmldate:sat, APR 2018 02:32:31 GMTEtag: " 588603ec-98b "Last-modified:mon, 13:23:56 gmtpragma:no-cacheserver:bfe/1.0.8.18set-cookie:bdorz=27315; max-age=86400; domain=.baidu.com; path=/<! DOCTYPE html><!--STATUS ok-->

  

3. Export the contents of the linked page to a local file

Curl https://www.baidu.com > Index.html

You can also save the content to a file by using the -o /option of Curl -O .

    •   -o(Lowercase O): Results are saved to the file name provided on the command line

-O(O in uppercase): The file name in the URL will be used as the file name to save the output

Curl-o index.html https://www.baidu.com
Curl-o http://www.hbygxh.org/html/2018/gyzxx_0309/857.html #结果, content is stored in a file with 857.html file name

Note: When you use -O options, you must make sure that the link ends with a file name, or curl does not save the file correctly. If you encounter a case where no file name is in the link, you should make

-o manually specify the file name with options, or use the redirect symbol.

4. Download multiple files simultaneously

We can use the -o or -O option to specify multiple links at the same time, writing commands in the following format:

Curl-o http://http://www.baidu.com/page/3/

Or:

Curl-o page1.html http://http://www.baidu.com/page/2/

5. Redirect with-l follow link

If you use Curl to open some redirected links, you won't be able to get the Web content we want in this case. For example:

    Curl http://codebelief.com

You will get the following prompt:

301 Moved permanently</title>"  White "> <center>301 Moved permanently1.10. 3</center> </body> 

And when we open the link through the browser, it automatically jumps to http://www.codebelief.com. What we want curl to do at this point is to follow the link's jump like a browser to get the final page content. We can add the-l option to the command to follow the link redirection:

    -L http://codebelief.com

This allows us to get the content of the redirected page.

6. Use-A to customize the User-agent

We can use to -A customize the user agent, for example, the following command will spoof cheer Firefox browser to request the webpage:

" mozilla/5.0 (Android; Mobile; rv:35.0) gecko/35.0 firefox/35.0" https://www.baidu.com

Below we will use-H to achieve the same purpose.

7. Use-h to customize the header

When we need to pass a particular header, we can write it in the following order:

" Referer:www.example.com " " user-agent:custom-user-agent " http://www.baidu.com

As you can see, we -H need to use the "" format when we use the user-agent to customize it User-Agent: xxx .

We are able to pass cookies directly in the header, in the same format as the example above:

" Cookie:jsessionid=d0112a5063d938586b659ef8f939be24 " http://www.example.com

Another approach is described below.

8. Save cookies with-C

When we use CURL to access the page, the Cookie is not saved by default. In some cases we would like to save cookies for use on our next visit. For example, landing a website, we want to visit the site again to maintain the status of landing, then we can now log on when the Cookie is saved, and then read the next visit.

-cfollow the name of the file you want to save.

" Cookie-example " http://www.example.com

9. Use-B to read cookies

The -H  method used to send a cookie is described earlier, in which the cookie string is written directly in the command. If you use-B to customize the Cookie, the command is as follows:

" Jsessionid=d0112a5063d938586b659ef8f939be24 " http://www.example.com

If you are unable to read cookie,-h from a file, you can use -b it at this time to achieve this goal:

    " Cookie-example " http://www.example.com

That is, it can be either -b  a cookie string or a file name that holds the cookie.

10. Send a POST request using-D

Let's take a landing page as an example of how to send a POST request using CURL. If you have a login page www.example.com/login, simply submit your username and password to log in. We can use CURL to complete this POST request, the-D is used to specify the data sent, and-X is used to specify how the data is sent:

Curl-d "username=tom&passwd=123456"-X POST Http://www.example.com/login

In -d the case of use, if omitted -X , the default is POST mode:

" username=tom&passwd=123456 " http://www.example.com/login

Force use of GET mode

" Somedata " -x GET http:///Www.example.com/api

Or use the -G option:

" Somedata " -G http:///Www.example.com/api

Read the data from the file

" @data. txt " http://www.example.com/login

Log in with a Cookie

Of course, if we visit the site again, it will still become a status of not logged in. We can save cookies in the way we mentioned earlier, and we will bring the cookie with you every time we visit our website to stay signed in.

" Cookie-login " " username=tom&passwd=123456 " http://www.example.com/login

When you visit the site again, use the following command:

    " Cookie-login " http://www.example.com/login

10 common usage Examples of command-line curl under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.