Skip to content

Chapter 5

Chapter 5

  • Response type
  • The response object can also have status_code, headers and media_types in addition to its content.
  • response_model can use a different object class as response type

    • the response_model parameter is thus effective in limiting the output data to that of the model. You can further trim the response with the help of the following additional parameters:
    • response_model_exclude_unset
    • response_model_include
    • response_model_exclude
  • Cookie: The set_cookie() method of the Response object makes setting a cookie very simple

  • write: add a response:Response object in the method, before return, response.set_cookie(key="some key", value=some_value).
  • read: add a parameter, whose name is some key, key_name: Cookie(None) to the method. See chap5/app.py for example.

  • Headers:

  • just as cookies, a web application may push a certain metadata in the form of HTTP headers into its response.
  • write: To set a custom header, its name should be prefixed with "X". In the example, the operation function adds a custom header called "X-Web-Framework", and a predefined header "Content-Language" along with the content to its response
  • read: to read the values of an HTTP header from the client request, import the Header class from the FastAPI library, and use its object as a parameter in operation function definition. The name of the parameter should match with the HTTP header converted in camel_case. If you try to retrieve the "accept-language" header, "-" in the name of identifier is replaced by "_".

  • Response Status

  • informational response: any respons with 1XX status code doesn't have a body
  • successful response: 200 is the default status code. The code 201 is also common, usually after creating a new resource
  • redirection messages: status code between 300 and 399 are used for redirection from one URL endpoint to anohter
    • 301 is included in the response letting the user know that the URL has been changed permanently
    • 307 indicated temporary redirect
  • client error response: The 4XX code imply client error responses
  • server error response: The codes starting with 5 represent the server errors.

  • Response type

  • HTMLResponse, JSONResponse, ORJSONRespons (a faster alternative), UJSONResponse.
  • StreamingResponse
  • FileResponse: note the open() function that returns the file object doesn't support async and await. Hence, the operation function cannot be a coroutine but a normal function. Use FileResponse class is more suitable for streaming a file as the application's response. It has a few arguments
    • path: the path to the file to stream
    • headers: you may include any custom headers if required
    • media_type: a string giving the media type
    • filename: if set, this will be included in the Content-Disposition response
  • RedirectResponse:
    • In FastAPI, the RedirectResponse class implements the HTTP redirect. By default, its status code is 307 -- indicating a temporary redirect.