10 common usage examples of cURL from the Linux command line
- 2020-06-12 11:35:40
- OfStack
preface
In Linux, curl is a file transfer tool that USES URL rules to work from the command line. It supports file uploads and downloads and is an integrated transfer tool, but traditionally has been referred to as url.
Grammar:
# curl [option] [url]
Here are 10 common usages of the Linux command line:
1. Get the page content
When we use curl without any options, the default is to send an GET request to get the linked content to standard output.
curl http://www.codebelief.com
2. Display the HTTP header
If we only want to show the HTTP header instead of the file content, we can use the -ES31en 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
It is also possible to display both the HTTP header and the file contents using the -ES39en 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
<!DOCTYPE html>
<html lang="en">
......
</html>
3. Save the link to the file
We can use > The symbol redirects the output to a local file.
curl http://www.codebelief.com > index.html
Also available via curl
-o
/
-O
Option to save the contents to a file.
-o
(lowercase o) : The result will be saved to the filename 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.codebelief.com
curl -O http://www.codebelief.com/page/2/
Note: Use
-O
Option, you must ensure that the filename is included at the end of the link, otherwise curl will not save the file properly. If you encounter a link without a file name, you should make
with
-o
Option to manually specify the file name, or use a redirection symbol.
4. Download multiple files simultaneously
We can use
-o
or
-O
Option to specify multiple links at the same time. 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. Follow the link redirection using -ES83en
If you use curl directly to open some of the redirected links, you won't get the content you want in this case. Such as:
curl http://codebelief.com
You will get the following prompt:
curl -I http://www.codebelief.com
0
And when we open the link via a browser, automatically jump to http: / / www codebelief. com. What we want curl to do at this point is follow the link like browser 1 and get the final page content. We can add the -L option to the command to follow the link redirection:
curl -L http://codebelief.com
This will allow us to retrieve the redirected content of the web page.
6. Use -ES105en to customize ES106en-ES107en
We can use
-A
Custom user agents, such as the following command, will make web page requests disguised as The Android Firefox browser:
curl -A "Mozilla/5.0 (Android; Mobile; rv:35.0) Gecko/35.0 Firefox/35.0" http://www.baidu.com
Next we will use -ES115en for the same purpose.
7. Customize header with -ES118en
When we need to pass a specific header, we can write as follows:
curl -I http://www.codebelief.com
3
You can see that when we use
-o
0
When customizing ES128en-ES129en, you need to use"
User-Agent: xxx
The format of ".
We can pass Cookie directly in header in the same format as in example 1 above:
curl -I http://www.codebelief.com
4
The other one is described below.
8. Save Cookie with -ES140en
When we use cURL to access pages, Cookie is not saved by default. In some cases, we want to save Cookie for our next visit. For example, if we log in a website and want to stay logged in when we visit the website again, we can now save Cookie when we logged in and read it the next time we visit.
-c
This is followed by the file name to save.
curl -I http://www.codebelief.com
5
9. Read Cookie using -ES155en
We talked about usage earlier
-H
To send Cookie by writing the Cookie string directly into the command. If using -ES163en to customize Cookie, the command is as follows:
curl -I http://www.codebelief.com
6
If you want to read Cookie from a file, -ES169en will not work and you can use it
-b
To achieve this goal:
curl -b "cookie-example" http://www.example.com
namely
-b
This can be followed by either the Cookie string or the file name that holds Cookie.
10. Send an POST request using -ES179en
Let's take the login page as an example to illustrate how to send POST requests using cURL. Suppose you have a login page www. example. com/login, only need to submit the user name and password to log in. We can use cURL to complete this 1 POST request, -ES192en to specify the data to be sent, -ES193en to specify how to send the data:
curl -d "userName=tom&passwd=123456" -X POST http://www.example.com/login
In the use of
-d
In the case if omitted
-X
, the default is POST mode:
curl -I http://www.codebelief.com
9
GET is mandatory
When sending data, POST can be used as well as GET, for example:
curl -d "somedata" -X GET http://www.example.com/api
Or use
-G
Options:
curl -d "somedata" -G http://www.example.com/api
Read data from a file
curl -d "@data.txt" http://www.example.com/login
Take Cookie login
Of course, if we visit the site again, it will remain unlogged. We can save Cookie using the method mentioned earlier and bring it with us every time we visit the site to keep us logged in.
curl -c "cookie-login" -d "userName=tom&passwd=123456" http://www.example.com/login
When you visit the site again, use the following command:
curl -b "cookie-login" http://www.example.com/login
This way, you can keep access to the logged in page.
conclusion