introduce
- The curl command is a file transfer tool that works on the command line using URL rules. Curl supports uploading and downloading files, so it is a comprehensive transfer tool. Curl is a powerful tool that supports HTTP, HTTPS, FTP and many other protocols. It also supports POST, cookies, authentication, downloading partial files from specified offsets, user agent strings, speed limits, file sizes, and progress bars. Curl can help automate web processing and data retrieval.
Simulate POST/GET requests
- Assume that the target URL is 127.0.0.1:8080/login
Delete from curl: delete from curl
$ curl http://127.0.0.1:8080/login?user=admin&passwd=12345678
Using curl to send a POST request
$ curl -d "user=admin&passwd=12345678" http://127.0.0.1:8080/login
Copy the code
HTTP headers can also be sent using curl. Use -h headers to send multiple headers
$ curl -H "Content-Type:application/json" http://127.0.0.1:8080
Copy the code
Use the -o option to write the downloaded data to a file and use –progress to display a progress bar
$curl http://127.0.0.1:8080 -o file.txt --progressCopy the code
Set cookies
- Use the –cookie “COKKIES” option to specify cookies, separating multiple cookies with semicolons
$curl http://127.0.0.1:8080 -- cookies"user=root; pass=123456"
Copy the code
Save the cookie as a file, using the –cookie-jar option
$curl http://127.0.0.1:8080 --cookie-jar cookie_fileCopy the code
Only the response header information is printed
- Only HTTP headers can be printed with -i or –head
$curl -i http://127.0.0.1:8080# Accept-Ranges: bytes
# Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
# Connection: Keep-Alive
# Content-Length: 277
# Content-Type: text/html
# Date: Thu, 29 Nov 2018 03:13:16 GMT
# Etag: "575e1f7b-115"
# Last-Modified: Mon, 13 Jun 2016 02:50:35 GMT
# Pragma: no-cache
# Server: bfe / 1.0.8.18
Copy the code
Set the user agent string with curl
- Curl curl curl curl curl curl curl curl curl curl curl curl curl Use the –user-agent or -a option
$ curl http://www.baidu.com --user-agent "Mozilla / 5.0"
$ curl http://www.baidu.com -A "Mozilla / 5.0"
Copy the code
More Advanced Uses