Seminar 5

5


Linux utils. XML, JSON, YAML. HTTP. URL. Curl.

Tasks

To start the server run:

$ docker run --rm -d -p 34567:1111 ghcr.io/devtools-course/task-http:latest

Now you can access the server via localhost, port 34567.

Part 0

Run a GET request to the / endpoint.

Solution
$ curl localhost:34567/
Hello, world!

Part 1

Run a GET request to the /e endpoint. What is the response code? Find a flag which makes curl fail when the request returns an error code.

Solution
$ curl --fail-with-body localhost:34567/e
Service Unavailable
curl: (22) The requested URL returned error: 503
$ echo $?
22

Part 2

Request body is defined by the following class:

data class pair(
    val x : Int,
    val y : Int,
)

Send a POST request to the /dj endpoint (x = 10 and y = 2): use JSON request body format.

Solution
$ curl --fail-with-body -X POST -H 'Content-Type: application/json' -d '{"x":10,"y":2}' localhost:34567/dj
{"result":5}

Part 3

Now use XML format. Parse the response so that the command output only the number (You are expected to write a bash pipeline). Use /dx endpoint and wrap the XML body in <req version="1.1">…​</req>.

Solution
$ curl --fail-with-body -X POST -H 'Content-Type: application/json' -d '<req version="1.1"><pair><x>10</x><y>2</y></pair></req>' localhost:34567/dx
<response><result>5</result></response>
$ curl --fail-with-body -X POST -H 'Content-Type: application/json' -d '<req version="1.1"><pair><x>10</x><y>2</y></pair></req>' localhost:34567/dx | sed -E 's|[^0-9]*([0-9]+)[^0-9]*|\1|g'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    95  100    40  100    55  26560  36520 --:--:-- --:--:-- --:--:-- 95000
5
$ curl --fail-with-body -X POST -H 'Content-Type: application/json' -d '<req version="1.1"><pair><x>10</x><y>2</y></pair></req>' localhost:34567/dx 2> /dev/null | sed -E 's|[^0-9]*([0-9]+)[^0-9]*|\1|g'
5