Skip to content

Instantly share code, notes, and snippets.

@rusrushal13
Last active September 10, 2019 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rusrushal13/2b57a8db963000ccf895dfdd4093813c to your computer and use it in GitHub Desktop.
Save rusrushal13/2b57a8db963000ccf895dfdd4093813c to your computer and use it in GitHub Desktop.
Curl Exercises are given here: https://jvns.ca/blog/2019/08/27/curl-exercises/

Solution to Curl Excersises

  • Request https://httpbin.org

curl https://httpbin.org

<!DOCTYPE html>
<html lang="en">

<head>...
  • 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.

curl https://httpbin.org/anything

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "GET",
  "origin": "180.151.249.94, 180.151.249.94",
  "url": "https://httpbin.org/anything"
}
  • Make a POST request to https://httpbin.org/anything

curl -X POST https://httpbin.org/anything

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "0",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "POST",
  "origin": "180.151.249.94, 180.151.249.94",
  "url": "https://httpbin.org/anything"
}
  • Make a GET request to https://httpbin.org/anything, but this time add some query parameters (set value=panda).

curl https://httpbin.org/anything?value=panda

{
  "args": {
    "value": "panda"
  },
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "GET",
  "origin": "180.151.249.94, 180.151.249.94",
  "url": "https://httpbin.org/anything?value=panda"
}

curl https://www.google.org/robots.txt

User-agent: *
Disallow: /search
Allow: /search/about
Allow: /search/static
Allow: /search/howsearchworks
Disallow: /sdch
Disallow: /groups
Disallow: /index.html?
...

# AdsBot
User-agent: AdsBot-Google
Allow: /maps/api/js?
Disallow: /maps/api/js/
...

# Certain social media sites are whitelisted to allow crawlers to access page markup when links to google.org/imgres* are shared. To learn more, please contact images-robots-whitelist@google.org.
User-agent: Twitterbot
Allow: /imgres

User-agent: facebookexternalhit
Allow: /imgres

Sitemap: https://www.google.org/sitemap.xml
  • Make a GET request to https://httpbin.org/anything and set the header User-Agent: elephant.

curl https://httpbin.org/anything -H "User-agent: elephant"

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "elephant"
  },
  "json": null,
  "method": "GET",
  "origin": "180.151.249.94, 180.151.249.94",
  "url": "https://httpbin.org/anything"
}
  • Make a DELETE request to https://httpbin.org/anything

curl -X DELETE https://httpbin.org/anything

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "DELETE",
  "origin": "180.151.249.94, 180.151.249.94",
  "url": "https://httpbin.org/anything"
}
  • Request https://httpbin.org/anything and also get the response headers

curl -i https://httpbin.org/anything For only response header, use -I flag.

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: application/json
Date: Wed, 28 Aug 2019 07:27:18 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: 292
Connection: keep-alive

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "GET",
  "origin": "180.151.249.94, 180.151.249.94",
  "url": "https://httpbin.org/anything"
}
  • Make a POST request to https://httpbin.org/anything with the JSON body {"value": "panda"}

curl -X POST https://httpbin.org/anything -d '{"value": "panda"}'

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "{\"value\": \"panda\"}": ""
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "18",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "POST",
  "origin": "180.151.249.94, 180.151.249.94",
  "url": "https://httpbin.org/anything"
}
  • 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 the json field in the response to see the difference from the previous one.

curl -X POST https://httpbin.org/anything -H 'Content-Type: application/json' -d '{"value": "panda"}'

{
  "args": {},
  "data": "{\"value\": \"panda\"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "18",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": {
    "value": "panda"
  },
  "method": "POST",
  "origin": "180.151.249.94, 180.151.249.94",
  "url": "https://httpbin.org/anything"
}
  • Make a GET request to https://httpbin.org/anything and set the header Accept-Encoding: gzip (what happens? why?)

curl https://httpbin.org/anything -H 'Accept-Encoding: gzip'

Use Accept-Encoding: gzip,deflate header if you're getting gzip encoded data as a response.

U�A� E����4
�hZݹ0��PE�Q0���x�Zc���?3�&H�0�'kb�����=�F�� [Jj�����(���-���ݰ���`�v~�z�i��\ ������`T,�jT>�4A�|���RX*Ʈ[���V�n�Z޷W�⌻�/����
                                                                                                                        eI����~�t��&�mZ�7{_���D%
  • 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

curl -X POST https://httpbin.org/anything -d @data.json

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "{\"value\": \"panda\"}": ""
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "18",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "POST",
  "origin": "180.151.249.94, 180.151.249.94",
  "url": "https://httpbin.org/anything"
}
  • 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 with different Accept: headers.

curl https://httpbin.org/image

{
  "message": "Client did not request a supported media type.",
  "accept": [
    "image/webp",
    "image/svg+xml",
    "image/jpeg",
    "image/png",
    "image/*"
  ]
}

curl https://httpbin.org/image -H 'Accept: image/png' -o image.png

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 10568  100 10568    0     0   8799      0  0:00:01  0:00:01 --:--:--  8799
  • Make a PUT request to https://httpbin.org/anything

curl -X PUT https://httpbin.org/anything

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "0",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "PUT",
  "origin": "180.151.249.94, 180.151.249.94",
  "url": "https://httpbin.org/anything"
}
  • Request https://httpbin.org/image/jpeg, save it to a file, and open that file in your image editor.

curl https://httpbin.org/image/jpeg -o image.jpeg

  • Request https://google.org. 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.

curl -i https://google.org

The request page is redirected to https://www.google.com because getting the 302 http status and the response headers are implying the same.

HTTP/2 302
location: https://www.google.org/
cache-control: private
content-type: text/html; charset=UTF-8
x-content-type-options: nosniff
date: Fri, 30 Aug 2019 09:02:25 GMT
server: sffe
content-length: 220
x-xss-protection: 0
alt-svc: quic=":443"; ma=2592000; v="46,43,39"

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://www.google.org/">here</A>.
</BODY></HTML>
  • Make any request to https://httpbin.org/anything and just set some nonsense headers (like panda: elephant)

curl https://httpbin.org/anything -H 'panda: elephant'

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "Panda": "elephant",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "GET",
  "origin": "106.51.36.100, 106.51.36.100",
  "url": "https://httpbin.org/anything"
}
  • Request https://httpbin.org/status/404 and https://httpbin.org/status/200. Request them again and get curl to show the response headers.

curl https://httpbin.org/status/404 -i

HTTP/1.1 404 NOT FOUND
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=utf-8
Date: Mon, 02 Sep 2019 09:48:11 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: 0
Connection: keep-alive

curl https://httpbin.org/status/200 -i

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: text/html; charset=utf-8
Date: Mon, 02 Sep 2019 09:48:27 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: 0
Connection: keep-alive

  • Request https://httpbin.org/anything and set a username and password (with -u username:password)

curl https://httpbin.org/anything -u username:password

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "GET",
  "origin": "106.51.36.100, 106.51.36.100",
  "url": "https://httpbin.org/anything"
}

curl https://httpbin.org/anything -u username:password -i

HTTP/1.1 200 OK
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Type: application/json
Date: Mon, 02 Sep 2019 09:50:05 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: 346
Connection: keep-alive

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.54.0"
  },
  "json": null,
  "method": "GET",
  "origin": "106.51.36.100, 106.51.36.100",
  "url": "https://httpbin.org/anything"
}
  • Download the Twitter homepage (https://twitter.com) in Spanish by setting the Accept-Language: es-ES header.

curl https://twitter.com -H 'Accept-Language: es-ES'

<!DOCTYPE html>
<html lang="es" data-scribe-reduced-action-queue="true">
  <head>
    <meta charset="utf-8">
      <script  nonce="MsO6XYsUgK45wl2lOPWgnQ==">
        !function(){window.initErrorstack||(window.initErrorstack=[]),window.onerror=function(r,i,n,o,t){r.indexOf("Script error.")>-1||window.initErrorstack.push({errorMsg:r,url:i,lineNumber:n,column:o,errorObj:t})}}();
      </script>
...
<title>Twitter. Es lo que está pasando.</title>
...
<input type="hidden" id="swift-module-path" value="https://abs.twimg.com/k/swift/es">
    <script src="https://abs.twimg.com/k/es/init.es.001a27a19c4e9728dd35.js" async></script>
  </body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment