21 curl exercises
21 curl exercises
Reference:https://jvns.ca/blog/2019/08/27/curl-exercises/
These exercises are about understanding how to make different kinds of HTTP requests with curl. They’re a little repetitive on purpose. They exercise basically everything I do with curl.
To keep it simple, we’re going to make a lot of our requests to the same website: https://httpbin.org. httpbin is a service that accepts HTTP requests and then tells you what request you made.
- Request https://httpbin.org
- Request https://httpbin.org/anything. httpbin.org/anything will look at the request you made, parse it, and echo back to you what you requested. curl’s default is to make a GET request.
- Make a POST request to https://httpbin.org/anything
- Make a GET request to https://httpbin.org/anything, but this time
add some query parameters (set
value=panda). - Request google’s robots.txt file (www.google.com/robots.txt)
- Make a GET request to https://httpbin.org/anything and set the
header
User-Agent: elephant. - Make a DELETE request to https://httpbin.org/anything
- Request https://httpbin.org/anything and also get the response headers
- Make a POST request to https://httpbin.org/anything with the JSON
body
{"value": "panda"} - Make the same POST request as the previous exercise, but set the
Content-Type header to
application/json(because POST requests need to have a content type that matches their body). Look at thejsonfield in the response to see the difference from the previous one. - Make a GET request to https://httpbin.org/anything and set the
header
Accept-Encoding: gzip(what happens? why?) - Put a bunch of a JSON in a file and then make a POST request to https://httpbin.org/anything with the JSON in that file as the body
- Make a request to https://httpbin.org/image and set the header
‘Accept: image/png’. Save the output to a PNG file and open the file in
an image viewer. Try the same thing with different
Accept:headers. - Make a PUT request to https://httpbin.org/anything
- Request https://httpbin.org/image/jpeg, save it to a file, and open that file in your image editor.
- Request https://www.twitter.com. You’ll get an empty response. Get curl to show you the response headers too, and try to figure out why the response was empty.
- Make any request to https://httpbin.org/anything and just set some
nonsense headers (like
panda: elephant) - Request https://httpbin.org/status/404 and https://httpbin.org/status/200. Request them again and get curl to show the response headers.
- Request https://httpbin.org/anything and set a username and password
(with
-u username:password) - Download the Twitter homepage (https://twitter.com) in Spanish by
setting the
Accept-Language: es-ESheader. - Omitted!
1~5
1 | curl httpbin.org |
6~10
1 | curl httpbin.org/anything -H "User-Agent:elephant" # 6a |
11~15
1 | curl httpbin.org/anything -H "Accept-Encoding:gzip" |
16~20
1 | curl https://www.twitter.com -I # 303 Moved Permanently |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Do not touch fish!!