Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Examples of 10 common uses of command line cURL under Linux

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface

In Linux, curl is a file transfer tool that uses URL rules to work under the command line, and it can be said to be a very powerful http command line tool. It supports uploading and downloading of files, and is an integrated transfer tool, but traditionally, url is called a download tool.

Syntax: # curl [option] [url]

This article mainly shares 10 common uses of Linux command line cURL for your reference. Let's take a look at the detailed introduction:

1. Get page content

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

Curl http://www.codebelief.com

two。 Show HTTP header

If we want to display only the HTTP header and not the contents of the file, we can use the-I option:

Curl-I http://www.codebelief.com

The output is:

HTTP/1.1 200 OK Server: nginx/1.10.3 Date: Thu, 11 May 2017 08:24:45 GMT Content-Type: text/html; charset=utf-8 Content-Length: 24206 Connection: keep-alive X-Powered-By: Express Cache-Control: public, max-age=0 ETag: W / "5e8e-Yw5ZdnVVly9/aEnMX7fVXQ" Vary: Accept-Encoding

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

Curl-I http://www.codebelief.com

The output is:

HTTP/1.1 200 OK Server: nginx/1.10.3 Date: Thu, 11 May 2017 08:25:46 GMT Content-Type: text/html; charset=utf-8 Content-Length: 24206 Connection: keep-alive X-Powered-By: Express Cache-Control: public, max-age=0 ETag: W / "5e8e-Yw5ZdnVVly9/aEnMX7fVXQ" Vary: Accept-Encoding.

3. Save the link to a file

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

Curl http://www.codebelief.com > index.html

You can also save the content to a file through the-o _ curl option that comes with it.

-o (lowercase o): the result will be saved to the file name provided on the command line-O (uppercase O): the file name in URL will be used as the file name curl-o index.html http://www.codebelief.com curl-O http://www.codebelief.com/page/2/ to save the output

Note: when using the-O option, you must make sure that the file name is included at the end of the link, otherwise curl will not save the file correctly. If there is no file name in the link, you should make the

Specify the file name manually with the-o option, or use the redirect symbol.

4. Download multiple files at the same time

We can use the-o or-O options to specify multiple links at the same time and write commands in the following format:

Curl-O http://www.codebelief.com/page/2/-O http://www.codebelief.com/page/3/

Or:

Curl-o page1.html http://www.codebelief.com/page/1/-o page2.html http://www.codebelief.com/page/2/

5. Use-L to follow the link redirection

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

Curl http://codebelief.com

You will be prompted as follows:

301 Moved Permanently 301 Moved Permanently nginx/1.10.3

When we open the link through a browser, we automatically jump to http://www.codebelief.com. What we want curl to do at this point is to follow 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 http://codebelief.com

In this way, we can get the redirected web content.

6. Use-A to customize User-Agent

We can use-A to customize the user agent, such as the following command to request a web page disguised as Android Firefox:

Curl-A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" http://www.baidu.com

Next we will use-H to achieve the same goal.

7. Use-H to customize header

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

Curl-H "Referer: www.example.com"-H "User-Agent: Custom-User-Agent" http://www.baidu.com

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

We can pass Cookie directly in header, in the same format as the example above:

Curl-H "Cookie: JSESSIONID=D0112A5063D938586B659EF8F939BE24" http://www.example.com

Another way is described below.

8. Use-c to save Cookie

When we use cURL to access the page, the default is not to save Cookie. In some cases we want to save the Cookie so that we can use it the next time we visit. For example, if we log on to a website, we want to keep the login status when we visit the site again, so we can save the login Cookie now and read it the next time we visit.

-c followed by the name of the file to be saved.

Curl-c "cookie-example" http://www.example.com

9. Use-b to read Cookie

I talked about the method of using-H to send Cookie, which is to write the Cookie string directly into the command. If you use-b to customize the Cookie, the command is as follows:

Curl-b "JSESSIONID=D0112A5063D938586B659EF8F939BE24" http://www.example.com

There is nothing you can do if you want to read Cookie,-H from a file, and you can use-b to do this at this point:

Curl-b "cookie-example" http://www.example.com

That is,-b can be followed by either a Cookie string or a file name with Cookie saved.

10. Use-d to send a POST request

Let's take a landing page as an example to illustrate how to use cURL to send POST requests. Suppose you have a login page, www.example.com/login, where you only need to submit a 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 how the data will be sent:

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

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

Curl-d "userName=tom&passwd=123456" http://www.example.com/login

Mandatory use of GET mode

When sending data, you can use not only POST but also GET, for example:

Curl-d "somedata"-X GET http://www.example.com/api

Or use the-G option:

Curl-d "somedata"-G http://www.example.com/api

Read data from a file

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

Log in with Cookie

Of course, if we visit the site again, it will still become unlogged in. We can save the Cookie using the method mentioned earlier and bring the Cookie with us every time we visit the site to maintain our login status.

Curl-c "cookie-login"-d "userName=tom&passwd=123456" http://www.example.com/login

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

Curl-b "cookie-login" http://www.example.com/login

In this way, you can keep visiting the page after login.

Summary

The above is the whole content of this article, I hope that the content of this article can bring some help to your study or work, if you have any questions, you can leave a message and exchange, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report