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.
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=5ton=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.
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-Typeheader and send again. You should get415 Unsupported Media Type. Add it back to fix the request. Now change the method toGET: what does405 Method Not Allowedtell you? Look for theallowresponse 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
/sumwith/averageor/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.