You can use Postman or cURL. Come in and see the unique tricks of cURL

Articles have been included in Github.com/niumoo/JavaNotes Welcome to Star and Instructions.
Welcome to my attention Public Number , articles are updated weekly.

cURL It is an open source free project, mainly command line tools cURL and libcurl, which 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, RTMPS, RTMPS, RTSP, SCP, SFTP, SMB, SMTP, SMTPS, TELNET, and TFTP.View the cURL source code to access the official Github.

What about installing cURL s?

ubuntu / Debian.

sudo apt install curl

CentOS / Fedora.

sudo yum install curl

Windows.

If you already have Git installed, Git Bash comes with a cURL. If you don't have a git as a developer, you can only make it official Manual Download.

1. Request Source

Direct curl.

$ curl http://wttr.in/

The example web address requested above is a weather website, which is interesting and will return the weather information for your location based on the ip information you requested.

When I wrote this, it was raining in my city of Shanghai, and it was raining all the time outside the window.

2. File Download

Save the file with -o, similar to the wget command, such as downloading README text and saving it asReadme.txtFile.If you need a custom file name, you can 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

The download file shows the download status, such as the size of the data volume, transfer speed, remaining time, and so on.You can disable the schedule using 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

You can also use the--process-bar parameter to display the schedule as a progress bar.

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

cURL is a powerful pronoun, breakpoints are passed on naturally, using the -C - parameter.Below is an example of a breakpoint download ubuntu20.04 image.

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

What?Don't want to take up too much network speed while downloading?Use --limit-rate to limit speed.

curl -C - -O https://mirrors.nju.edu.cn/ubuntu-releases/20.04/ubuntu-20.04-desktop-amd64.iso --limit-rate 100k

What?Do you want to download files from the FTP server again?Don't panic.

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

3. Response Headers

Display Response Headers information using the -i parameter.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: *

4. Request method (GET/POST/...)

Use-X to easily change the request mode.

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

5. Request parameters

Take the example of unread code with the name value of the incoming parameter.

Get mode parameter Direct url stitching parameter.

$ curl -X GET http://wttr.in?name=Unread Code

Post uses--data to set parameters.

$ curl -X POST --data "name=Unread Code" http://wttr.in

You can also customize the header parameter when you request it, adding it using --harder.

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

6. File upload

CURLs are much more powerful than that. Form submissions and file uploads are also optional. All you need to do is use the -F or -D parameter, -F automatically adds the request header Content-Type: multipart/form-data, while -D is Content-Type: application/x-www-form-urlencoded.

Like uploading oneProtrait.jpgPicture.

$ curl -F profile=@portrait.jpg https://example.com/upload

Submit a form with name and age parameters.

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

The corresponding content of the parameter can also be read from the file.

curl -F "content=<Darcy's birth.txt" https://example.com/upload

The content type is also specified when uploading.

curl -F "content=<Darcy's birth.txt;type=text/html" https://example.com/upload

Upload file with other parameters.

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

7. Web site wildcards

CURLs can match multiple web addresses, you can use {} with comma-separated to identify a segment in the url, or you can use [] to represent a surround parameter.

# requestWww.baidu.comandPan.baidu.comandFanyi.baidu.com
$ curl http://{www,pan,fanyi}.baidu.com
# Fictitious web addresses beginning with 1-10Baidu.comAnd then request
$ curl http://[1-10].baidu.com
# Fictitious web address starting with A-ZBaidu.comAnd then request
$ curl http://[a-z].baidu.com

This can sometimes be useful, for example, if you find the url pattern of a website.

8. Use cookie s

A response cookie is stored with the -c parameter at request time and -b allows you to carry the specified cookie with the request.

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

summary

These are the common uses of cURLs. Finally, let's tell you a little trick: browsers like Chrome and Firefox can directly copy requests for cURL statements.It's convenient to request the next test after saving.

Reference material

  1. https://curl.haxx.se/docs/manpage.html

Last words

Articles have been included in Github.com/niumoo/JavaNotes Welcome to Star and Instructions.More front-line factory interviews, Java programmers need to master the core knowledge and other articles, also collated a lot of my text, welcome Star and perfect, hope we can become excellent together.

Articles that are helpful and can be clicked "like" or "share" are all supported and I like them!
Articles are updated continuously every week. To keep an eye on my updated articles and shared dry goods in real time, you can focus on the Unread Code Public Number or My Blog.

Tags: Programming curl Ubuntu github ftp

Posted on Wed, 03 Jun 2020 22:28:42 -0400 by Tbull