[WIP]cURL Tutorial

created: 2023/1/23

https://www.booleanworld.com/curl-command-tutorial-examples/

cURL basic usage
 
curl <url>
Downloading Files with cURL
-o, --output <file>
curl -o vlc.dmg http://ftp.belnet.be/mirror/videolan/vlc/3.0.4/macosx/vlc-3.0.4.dmg
-O, --remote-name
curl -O http://ftp.belnet.be/mirror/videolan/vlc/3.0.4/macosx/vlc-3.0.4.dmg

Bear in mind that when you use the -o or the -O options and a file of the same name exists, cURL will overwrite it.

-C, --continue-at <offset>
curl -O -C - http://ftp.belnet.be/mirror/videolan/vlc/3.0.4/macosx/vlc-3.0.4.dmg

Use "-C -" to tell curl to automatically find out where/how to resume the transfer. 

 

Like most other command line tools, you can combine different options together. For example, in the above command, you could combine -O -C - and write it as -OC -

   
Anatomy of a HTTP request/response
   
   
Following redirects with cURL
-L, --location
curl -L http://www.facebook.com/
   
   
   
   
   
Viewing response headers with cURL
-i, --include
curl -L -i http://www.facebook.com/
   
Viewing request headers and connection details
-v, --verbose
curl -v https://www.booleanworld.com/
   
Silencing errors
-s, --silent
curl -svo /dev/null https://www.booleanworld.com/
-S, --show-error
curl -sSvo file.html https://www.booleanworld.com/

This option is global and does not need to be specified

   
Setting HTTP request headers with cURL
-H, --header <header/@file>
curl -H 'User-Agent: Mozilla/5.0' -H 'Host: www.google.com' ...

See also -A, --user-agent and -e, --referer.

-A, --user-agent <name>
curl -A Mozilla/5.0 http://httpbin.org/get

cURL does have certain shortcuts for frequently used flags. You can set the “User-Agent” header with the -A option

-e, --referer <URL>
curl --referer "https://fake.example" https://example.com

The “Referer” header is used to tell the server the location from which they were referred to by the previous site. It is typically sent by browsers when requesting Javascript or images linked to a page, or when following redirects. If you want to set a “Referer” header, you can use the -e flag:

   
Making POST requests with cURL
-d, --data <data>
curl --data "email=test%40example.com" https://httpbin.org/post

If the --data parameter is too big to type on the terminal, you can save it to a file and then submit it using @, like so:

curl --data @params.txt example.com
--data-urlencode <data>
curl --data-urlencode "email=test@example.com" --data-urlencode "name=Boolean World" https://httpbin.org/post

Alternatively, you can just use --data-urlencode to handle this for you. If you wanted to submit two parameters, email and name, this is how you should use the option:

-F, --form <name=content>
curl -F file=@test.c https://httpbin.org/post
   
Submitting JSON data with cURL

-d with 

-H 'Content-Type: application/json'
curl https://httpbin.org/post \
  -H 'Content-Type: application/json' \
  -d '{"email":"test@example.com", "name": ["Boolean", "World"]}'
 
curl --data @data.json https://httpbin.org/post

You can also save the JSON file, and submit it in the same way as we did previously:

   
Changing the request method
  WIP
   
   
   
   
   
Replicating browser requests with cURL
   
   
   
   
   
   
Making cURL fail on HTTP errors
   
   
   
   
   
   
Making authenticated requests with cURL
   
   
   
   
   
   
Testing protocol support with cURL
   
   
   
   
   
   
Setting the Host header and cURL’s --resolve option
   
   
   
   
   
   
Resolve domains to IPv4 and IPv6 addresses
   
   
   
   
   
   
Disabling cURL’s certificate checks
   
   
   
   
   
   
Troubleshooting website issues with “cURL timing breakdown”
   
   
   
   
   
   
cURL configuration files
   
   
   
   
   
   
Conclusion
   
   
   
   
   
   
posted @ 2023-01-23 17:59  懒虫哥哥  阅读(24)  评论(0编辑  收藏  举报