GETTING STARTED / LESSON 02

Methods, headers & JSON

Read a structured response, send a JSON body, and learn from the errors an API sends back.

An HTTP response has three useful parts: a status code, headers, and a body. In this lesson, response headers are expanded so you can compare them as you experiment.

Read a JSON response

Ask the Numbers API for five Fibonacci numbers. The format=json query parameter asks this API to return JSON instead of plain text.

Request headers 0

The explorer indents JSON to make its structure easier to read. Show raw switches back to the body as received. Look at content-type in the response headers.

Your turn: Change n=5 to n=8. Then remove &format=json. Compare both the body and the content type.

Send a body with POST

Some operations take more input than a URL comfortably holds. This endpoint accepts a JSON object with a list of numbers and computes their sum.

Request headers 1

Here, the request’s Content-Type tells the server that we are sending JSON. The response’s content-type tells us what the server sent back. They describe different messages.

Break it on purpose: Remove the request’s Content-Type header and send again. You should get 415 Unsupported Media Type. Add it back to fix the request. Now change the method to GET: what does 405 Method Not Allowed tell you? Look for the allow response header.

Make the same POST request with curl

-d supplies the body and implies POST. -H supplies a request header. -i asks to see the response headers as well as the body.

Explore: Replace /sum with /average or /sort, and change the numbers. Then try an empty list with /average. Read the error before changing anything else.

An error response is still a response. Keep the status, headers, and body together: each helps explain what happened.