Skip to content

Instantly share code, notes, and snippets.

@AlexGascon
Last active September 29, 2019 08:46
Show Gist options
  • Save AlexGascon/cc66442314407b4db287c36bf785e925 to your computer and use it in GitHub Desktop.
Save AlexGascon/cc66442314407b4db287c36bf785e925 to your computer and use it in GitHub Desktop.

curl exercises

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.

Exercise 1

Request https://httpbin.org

Command: curl https://httpbin.org

Exercise 2

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"
}

Exercise 3

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

Alternative solution

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 ""

Exercise 4

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

Alternative solution

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

Exercise 5

Request google’s robots.txt file (www.google.com/robots.txt)

Command:

curl www.google.com/robots.txt

Exercise 6

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

Exercise 7

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

Exercise 8

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment