You can subscribe if you search “App Alang” on wechat. Like and see, unlimited power.

This article Github.com/niumoo/Java… And program ape Alang blog has been included, there are many knowledge points and series of articles.

CURL is an open source, free project. It is mainly the command line tool cURL and libcurl. CURL can handle any network transport protocol, but does not involve any specific data processing.

CURL supports a wide range of communication protocols, such as DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP. Check out the cURL source code at Github.

What about installing cURL?

ubuntu / Debian.

sudo apt install curl
Copy the code

CentOS / Fedora.

sudo yum install curl
Copy the code

Windows.

Git Bash comes with cURL if you have Git installed. If you don’t have Git as a developer, you can only download it manually.

1. Request source code

The curl directly.

$ curl http://wttr.in/
Copy the code

The example url requested above is an interesting weather site that returns weather conditions for your location based on your requested IP information.

It is raining in Shanghai where I am writing this, and the rain is never ending outside my window.

2. Download files

Use -o to save the file, similar to the wget command, for example, download the README text and save it as a readme.txt file. If you need to customize the file name, use -o to customize the file name in the URL.

$ curl -o readme.txt https://mirrors.nju.edu.cn/kali/README
  %Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   159  100   159    0     0   1939      0 --:--:-- --:--:-- --:--:--  1939
Copy the code

Download the file will display the download status, such as data volume, transmission speed, remaining time, etc. Schedules can be disabled with the -s parameter.

$ curl -o readme.txt https://mirrors.nju.edu.cn/kali/README
  %Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   159  100   159    0     0   1939      0 --:--:-- --:--:-- --:--:--  1939
$ 
$ curl -o readme.txt https://mirrors.nju.edu.cn/kali/README -s
Copy the code

You can also use the –process-bar parameter to make the schedule appear as a progress bar.

$ curl -o readme.txt https://mirrors.nju.edu.cn/kali/README --progress-bar
## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # (100.0%)
Copy the code

CURL cURL cURL cURL cURL cURL cURL cURL cURL The following is an example of a breakpoint continuation to download the ubuntu20.04 image.

$The curl - O https://mirrors.nju.edu.cn/ubuntu-releases/20.04/ubuntu-20.04-desktop-amd64.iso - progress - bar
## 1.7%
^C
$The curl https://mirrors.nju.edu.cn/ubuntu-releases/20.04/ubuntu-20.04-desktop-amd64.iso - C - O - the progress bar
## # 2.4%
^C
$The curl https://mirrors.nju.edu.cn/ubuntu-releases/20.04/ubuntu-20.04-desktop-amd64.iso - C - O - the progress bar
## # 2.7%
^C
$ 
Copy the code

What? Don’t want to use too much Internet speed to download? Use –limit-rate to limit your speed.

The curl https://mirrors.nju.edu.cn/ubuntu-releases/20.04/ubuntu-20.04-desktop-amd64.iso - C - O - limit to 100 kCopy the code

What? Are you going to download files from FTP server again? Don’t panic.

curl -u user:password -O ftp://ftp_server/path/to/file/
Copy the code

3. Response Headers

Use the -i parameter to display Response Headers information. Use -i to display only Response Headers information.

$ curl -I http://wttr.in
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Sat, 30 May 2020 09:57:03 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 8678
Connection: keep-alive
Access-Control-Allow-Origin: *
Copy the code

4. Request mode (GET/POST/…)

Use -x to easily change the request style.

$ curl -X GET http://wttr.in
$ curl -X POST http://wttr.in
$ curl -X PUT http://wttr.in.Copy the code

5. Request parameters

Take the name value as an example.

Get parameter Indicates the PARAMETER of URL concatenation.

$curl -X GET http://wttr.in? Name = Program ape Aaron
Copy the code

Post uses –data to set parameters.

$ curl -X POST --data "Name = Program Alun" http://wttr.in
Copy the code

You can also customize the header parameter when requesting it, using –harder.

$ curl --header "Content-Type:application/json" http://wttr.in
Copy the code

6. Upload files

CURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL application/x-www-form-urlencoded.

For example, upload a protrait. JPG image.

$ curl -F [email protected] https://example.com/upload
Copy the code

Submit a form with name and age parameters.

curl -F name=Darcy -F age=18 https://example.com/upload
Copy the code

Parameters can also be read from the file.

The curl -f "content =" the story of darcy. TXT "https://example.com/uploadCopy the code

Specify the content type when uploading.

Curl -f "content=< d 'Arcy.txt; type=text/html" https://example.com/uploadCopy the code

Upload file along with other parameters.

curl -F 'file=@"localfile"; filename="nameinpost"' example.com/uploadCopy the code

7. Web address matching

CURL can implement multiple url matches, you can use {} in combination with comma-separated used to identify a section in the url, you can also use [] to represent the range of parameters.

#Request www.baidu.com and pan.baidu.com and fanyi.baidu.com
$ curl http://{www,pan,fanyi}.baidu.com
#Fictitious url 1-10 at the beginning of Baidu.com, then request
$ curl http://[1-10].baidu.com
#Fictitious url a-Z at the beginning of Baidu.com, then request
$ curl http://[a-z].baidu.com
Copy the code

This can be useful sometimes, for example when you discover a url pattern for a website.

8. Using cookies

The -c parameter is used to store the cookie of the response while the -b parameter can be used to carry the specified cookie with the request.

$ curl -c wdbyte_cookies http://www.wdbyte.com
$ curl -b wdbyte_cookes http://www.wdbyte.com
Copy the code

conclusion

CURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL cURL After saving, it is very convenient to test the next request.

The resources

  1. The curl. Haxx. Se/docs/manpag…

Hello world:) I’m Aaron, a tech tool guy on the front line. The article continues to update, can pay attention to the public number “program ape A Lang” grow together. This article has been received at Github.com/niumoo/Java… And “Unread Code,” lots of knowledge and a series of articles.