new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Tutorial · all subjects

cookie-parameters

10 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Cookie parameter models with Pydantic

You can create a Pydantic model to declare a group of related cookies. This allows you to re-use the model in multiple places and declare validations and metadata for all cookie parameters at once. You declare the cookie parameters in a Pydantic model, then declare the parameter as Cookie. FastAPI will extract the data for each field from the cookies received in the request and give you the Pydantic model you defined.

Cookie parameter models supported from FastAPI 0.115.0

Cookie parameter models using Pydantic are supported since FastAPI version 0.115.0.

Cookie parameter model technique applies to Query and Header too

The same technique of using Pydantic models for parameter declaration applies to Query, Cookie, and Header.

Forbid extra cookies with Pydantic model configuration

You can use Pydantic's model configuration to forbid any extra fields in cookies. When configured to forbid extra cookies, if a client sends cookies that are not declared in the model, they will receive an error response with type 'extra_forbidden', location path ['cookie', '<cookie_name>'], and message 'Extra inputs are not permitted'.

Cookies not sent in API docs UI due to JavaScript restrictions

Browsers handle cookies in special ways that prevent JavaScript from easily accessing them. When using the API docs UI at /docs, the documentation for cookies is visible, but even if you fill in cookie data and click Execute, the cookies will not actually be sent because the docs UI uses JavaScript. This will result in an error message as if no values were written.

Import Cookie from fastapi

Import Cookie from fastapi to declare cookie parameters in your path operations.

Cookie parameters use same structure as Query and Path

Cookie parameters are declared using the same structure as Path and Query parameters. You can define default values as well as all extra validation or annotation parameters for cookie parameters.

Cookie is a sister class of Path and Query

Cookie is a sister class of Path and Query. It inherits from the same common Param class. When imported from fastapi, Cookie is actually a function that returns a special class.

Must use Cookie to prevent parameters being interpreted as query parameters

To declare cookies, you must use Cookie, because otherwise the parameters would be interpreted as query parameters.

Cookies in Swagger UI docs do not actually send due to JavaScript limitations

Browsers handle cookies in special ways and JavaScript cannot easily touch them. While the API docs UI at /docs shows documentation for cookies in path operations, if you fill in data and click Execute, the cookies won't actually be sent due to the docs UI using JavaScript. This will appear as an error message.

Give your agent this brain