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

How to use the curl command on Linux system

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the Linux system curl command how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Curl is a download tool in Linux system, which mainly uses URL rules to upload and download files. It is a comprehensive transmission tool.

Syntax: curl [option] [url] Common parameters:-A/--user-agent string > set user agent to send to the server-b/--cookie name=string/file > cookie string or file read location-c/--cookie-jar file > write cookie to this file after the operation-C/--continue-at offset > Breakpoint resume-D/--dump-header file > write header information to this file-e/--referer source URL-http error is not displayed when f/--fail connection fails-o/--output Write the output to the file-O/--remote-name writes the output to the file Keep the file name of the remote file-r/--range range > retrieve the byte range from the HTTP/1.1 or FTP server-s/--silent mute mode. Do not output anything-T/--upload-file file > upload files-u/--user user [: password] > set the user and password of the server-w/--write-out [format] what output is complete-x/--proxy host [: port] > use the HTTP agent-# / on a given port -- the progress-bar progress bar shows the current delivery status case demonstration: 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.w``.codebelief.com2. 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.w``.codebelief.com is output as:

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 can also display both HTTP headers and file contents, using the-I option:

Curl-I http: ``/ / www.w``.codebelief.com is output as:

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.w``.codebelief.com > index.html can also save the content to a file through the-oUnio option that comes with curl.

-o (lowercase o): the result is 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 to save the output

Curl-o index.html http: ``/ / www.w``.codebelief.com ``curl-O http:`` / / www.w``.codebelief.com`` / 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.w``.codebelief.com`` / page/2/``-O http: ``/ / www.w``.codebelief.com`` / page/3/ or:

Curl-o page1.html http: ``/ / www.w``.codebelief.com`` / page/1/``-o page2.html http: ``/ / www.w``.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 will be prompted as follows:

301 Moved Permanently 301 Moved Permanently nginx/1.10.3 and when we open the link through a browser, it automatically jumps 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 so that 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:

Under curl-A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" http://www.baidu.com, we will use-H to achieve the same purpose.

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 can see that 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.w``.example.com another method will be 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.com9. 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.w``.example.com is powerless if you want to read Cookie,-H from a file. You can use-b to achieve this purpose:

Curl-b ``"cookie-example"`` http: ``/ / www.w``.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.w``.example.com`` / login if-d is used, if-X is omitted, the default is POST:

Curl-d ``"userName=tom&passwd=123456" ``http:`` / / www.w``.example.com`` / login enforce the 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.w``.example.com`` / api or use the-G option:

Curl-d ``"somedata"``-G http: ``/ / www.w``.example.com`` / api reads data from the file

Curl-d ``"@ data.txt" ``http:`` / / www.w``.example.com`` / login login 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.

When curl-c ``"cookie-login"``-d ``"userName=tom&passwd=123456"`` http: ``/ www.w``.example.com`` / login visits the website again, use the following command:

Curl-b ``"cookie-login "``http: ``/ / www.w``.example.com`` / login so that you can keep visiting the login page.

Thank you for reading this article carefully. I hope the article "how to use curl commands in Linux system" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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

Development

Wechat

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

12
Report