]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:memo: Add Cookie docs
authorSebastián Ramírez <tiangolo@gmail.com>
Fri, 14 Dec 2018 16:44:42 +0000 (20:44 +0400)
committerSebastián Ramírez <tiangolo@gmail.com>
Fri, 14 Dec 2018 16:44:42 +0000 (20:44 +0400)
docs/tutorial/cookie-params.md [new file with mode: 0644]
docs/tutorial/src/cookie-params/tutorial001.py [new file with mode: 0644]
mkdocs.yml

diff --git a/docs/tutorial/cookie-params.md b/docs/tutorial/cookie-params.md
new file mode 100644 (file)
index 0000000..ba1756b
--- /dev/null
@@ -0,0 +1,29 @@
+You can define Cookie parameters the same way you define `Query` and `Path` parameteres.
+
+## Import `Cookie`
+
+First import `Cookie`:
+
+```Python hl_lines="1"
+{!./tutorial/src/cookie-params/tutorial001.py!}
+```
+
+## Declare `Cookie` parameteres
+
+Then declare the cookie parameters using the same structure as with `Path` and `Query`.
+
+The first value is the default value, you can pass all the extra validation or annotation parameteres:
+
+```Python hl_lines="7"
+{!./tutorial/src/cookie-params/tutorial001.py!}
+```
+
+!!! info
+    `Cookie` is a "sister" class of `Path` and `Query`. It also inherits from the same common `Param` class.
+
+!!! info
+    To declare cookies, you need to use `Cookie`, because otherwise the parameters would be interpreted as query parameteres.
+
+## Recap
+
+Declare cookies with `Cookie`, using the same common pattern as `Query` and `Path`.
diff --git a/docs/tutorial/src/cookie-params/tutorial001.py b/docs/tutorial/src/cookie-params/tutorial001.py
new file mode 100644 (file)
index 0000000..5a6fd30
--- /dev/null
@@ -0,0 +1,8 @@
+from fastapi import Cookie, FastAPI
+
+app = FastAPI()
+
+
+@app.get("/items/")
+async def read_items(*, ads_id: str = Cookie(None)):
+    return {"ads_id": ads_id}
index 84107b828c09ba15cf85921bdfbf368d9bd1cca8..d9b32f0f28a15c81af5ee0da7f853aa50ced34ee 100644 (file)
@@ -27,6 +27,7 @@ nav:
         - Body - Multiple Parameters: 'tutorial/body-multiple-params.md'
         - Body - Schema: 'tutorial/body-schema.md'
         - Body - Nested Models: 'tutorial/body-nested-models.md'
+        - Cookie Parameters: 'tutorial/cookie-params.md'
     - Concurrency and async / await: 'async.md'
     - Deployment: 'deployment.md'