In this gist I'll upload the solutions to the 21 curl exercises proposed by Julia Evans on this blogpost. While curl definitely isn't a life-changing skill, it's a tool that can be really useful once you master it, and considering the low investment that is required it's definitely a good time investment
All the solutions contain the command that needs to be run to solve the exercise; apart from that, some of them will include also the response that we've obtained, or a small explanation about the different arguments of the command that we've used. That will make it easier for people reading this to get a better understanding of the solution.
Request https://httpbin.org
Command: curl 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.
Command:
curl https://httpbin.org/anything
Response:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0"
},
"json": null,
"method": "GET",
"origin": "37.228.242.87, 37.228.242.87",
"url": "https://httpbin.org/anything"
}
Make a POST request to https://httpbin.org/anything
Command:
curl -X POST httpbin.org/anything
Response:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Length": "0",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0"
},
"json": null,
"method": "POST",
"origin": "84.203.51.67, 84.203.51.67",
"url": "https://httpbin.org/anything"
}
Explanation:
The -X
or --method
argument allows us to specify the HTTP request method that we want to use
Command:
curl -d "" httpbin.org/anything
Response: (The same as in the previous command)
Explanation:
The -d
argument is used to specify data to be sent on a POST request. Therefore, by adding it we're automatically using the POST method for our request.
As we don't want to send any data in the request, we're sending an empty string as the request body: -d ""
Make a GET request to https://httpbin.org/anything, but this time add some query parameters (set value=panda).
Command:
curl "https://httpbin.org/anything?value=panda"
Response:
{
"args": {
"value": "panda"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0"
},
"json": null,
"method": "GET",
"origin": "84.203.51.67, 84.203.51.67",
"url": "https://httpbin.org/anything?value=panda"
}
Explanation:
There isn't much to explain here: we just append the query parameters at the end of the URL manually
Command:
curl -G https://httpbin.org/anything -d "value=panda"
Response: (The same as in the previous command)
Explanation:
The -G
argument forces the request to use the GET method (similar to if we were using -X GET
). Therefore, when specifying data parameters lately with -d
, it appends them as query string parameters instead of sending them as POST data.
We can also specify several parameters by specifying several times the -d
argument. Example: curl -G -d "one=value" -d "another=value" https://httpbin.org/anything
Request google’s robots.txt file (www.google.com/robots.txt)
Command:
curl www.google.com/robots.txt
Make a GET request to https://httpbin.org/anything and set the header User-Agent: elephant
Command:
curl https://httpbin.org/anything -A elephant
Response:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "elephant"
},
"json": null,
"method": "GET",
"origin": "84.203.51.67, 84.203.51.67",
"url": "https://httpbin.org/anything"
}
Explanation:
The -A
or --user-agent
argument allows us to set the user agent in the request
Make a DELETE request to https://httpbin.org/anything
Command:
curl -X DELETE https://httpbin.org/anything
Response:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0"
},
"json": null,
"method": "DELETE",
"origin": "84.203.51.67, 84.203.51.67",
"url": "https://httpbin.org/anything"
}
Explanation:
As we saw in exercise 3, the -X
argument allows us to select which HTTP method we want to use
Request https://httpbin.org/anything and also get the response headers
Command:
curl -i https://httpbin.org/anything
Response:
HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: application/json
Date: Sun, 29 Sep 2019 08:45:19 GMT
Referrer-Policy: no-referrer-when-downgrade
Server: nginx
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
Content-Length: 288
Connection: keep-alive
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/7.58.0"
},
"json": null,
"method": "GET",
"origin": "84.203.51.67, 84.203.51.67",
"url": "https://httpbin.org/anything"
}
Explanation:
The -i
argument tells curl to include the response headers in the output