Update query param tests and docs

This commit is contained in:
Sebastián Ramírez
2018-12-18 23:38:38 +04:00
parent b59e8ffcf6
commit 4b94da956c
7 changed files with 272 additions and 12 deletions

View File

@@ -140,7 +140,7 @@ Here the query parameter `needy` is a required query parameter of type `str`.
If you open in your browser a URL like:
```
http://127.0.0.1:8000/users/2/items/foo-item
http://127.0.0.1:8000/items/foo-item
```
...without adding the required parameter `needy`, you will see an error like:
@@ -163,7 +163,7 @@ http://127.0.0.1:8000/users/2/items/foo-item
As `needy` is a required parameter, you would need to set it in the URL:
```
http://127.0.0.1:8000/users/2/items/foo-item?needy=sooooneedy
http://127.0.0.1:8000/items/foo-item?needy=sooooneedy
```
...this would work:
@@ -171,7 +171,18 @@ http://127.0.0.1:8000/users/2/items/foo-item?needy=sooooneedy
```JSON
{
"item_id": "foo-item",
"owner_id": 2,
"needy": "sooooneedy"
}
```
```
And of course, you can define some parameters as required, some as having a default value, and some entirely optional:
```Python hl_lines="7"
{!./tutorial/src/query_params/tutorial006.py!}
```
In this case, there are 3 query parameters:
* `needy`, a required `str`.
* `skip`, an `int` with a default value of `0`.
* `limit`, an optional `int`.

View File

@@ -3,7 +3,7 @@ from fastapi import FastAPI
app = FastAPI()
@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(user_id: int, item_id: str, needy: str):
item = {"item_id": item_id, "owner_id": user_id, "needy": needy}
@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str):
item = {"item_id": item_id, "needy": needy}
return item

View File

@@ -0,0 +1,9 @@
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/{item_id}")
async def read_user_item(item_id: str, needy: str, skip: int = 0, limit: int = None):
item = {"item_id": item_id, "needy": needy, "skip": skip, "limit": limit}
return item