HTTP and APIs from the shell

A request says what you want and a response says what happened. Most of the reading is in the status line and the headers, and the body is usually JSON that needs shaping before you can see its structure.

4 questions 1 task Read the lesson

Questions

Each one is marked as you go, and the explanation arrives straight after, which is the part worth reading.

4 questions, marked as you go.

1 What does curl -i add to the output?
2 What does a 404 status code mean?
3 Which curl option sends a body with the request?
4 Why is a JSON formatter such as python -m json.tool useful on a reply?

Tasks

These need your own device. Run the command in Termux, then paste what it printed. The marking looks at what the output means, so spacing, version numbers and ordering do not matter. If you have not installed anything yet, the lesson covers the setup.

Pretty-print a saved JSON reply

This writes a small JSON reply to a file, the way a saved API response would sit on disk, then reformats it so each key and value is on its own line. The lesson writes the interpreter as python; Termux also ships the versioned name python3, which is what this uses.

Run this in Termux On your own device. Nothing on this page executes it. The command runs in your terminal, and this box only reads what you paste back.

printf "{\"id\": 42, \"name\": \"ada\", \"created\": true}\n" > ~/people.json; python3 -m json.tool ~/people.json
Marking your own work Both halves of this are checked in your browser, with no server involved, so nothing stops you from pasting output you did not run. The tasks are written so that the easiest way to pass one is to actually do it and read the result, which is the whole intention.

All sets Read the lesson