HTTP and Flask Basics – HTTP Responses
After the request has been received by the server and processed, the server returns an HTTP response message to the client. The response informs the client of the outcome of the requested operation.
- Status Code & Status Message
- HTTP Version
Headers: similar to the request headers, provides information about the response and resource representation. Some common headers include:
- Content-Type: the media type of the body of the request
- Body: optional data containing the requested resource.
As an API developer, it’s important to send the correct status code. As a developer using an API, the status codes - particularly the error codes - are important for understanding what caused and error and how to proceed.
Codes fall into five categories
- 1XX: Informational
- 2XX: Successful response
- 3XX: Redirection (Something happen and there was a redirection and the back end)
- 4XX: Client Error
- 5XX: Server Error (The client sent a request that was fine but something happened in the back end such that the server couldn’t successfully process and needs to let the user know that.)
- 100: Continue (You would see for that options request which basically says, “I got the options you have permission to process, continue sending your request.”)
- 200: OK (For the GET request)
- 201: Created (That is going to go with a POST request.)
- 304: Not Modified (If a PUT or PATCH request was unsuccessful)
- 400: Bad Request (This is one of those client errors, it means something about that request format was wrong and the server could not handle it.)
- 401: Unauthorized (The request was formatted fine but you don’t have access to what you wanted to do or to that resource either the operation or the resource or both)
- 404: Not Found (Whatever resource you were requesting doesn’t actually exist in the back end.)
- 500: Internal Server Error (This one is broad category of something happened in the server while attempting to process a valid request from the client and it was not able to be completed.)