背景
- 前面讲了可以自定义 Response,那么这里就讲下请求对象 Request
- 可以通过 Request 来获取一些数据
获取请求基础信息
@app.get("/base") async def get_base(*, request: Request): res = { # 客户端连接的 host "host": request.client.host, # 客户端连接的端口号 "port": request.client.port, # 请求方法 "method": request.method, # 请求路径 "base_url": request.base_url, # request headers "headers": request.headers, # request cookies "cookies": request.cookies } return res
请求结果
{ "host": "127.0.0.1", "port": 54364, "method": "GET", "base_url": { "_url": "http://127.0.0.1:8080/" }, "headers": { "host": "127.0.0.1:8080", "connection": "keep-alive", "sec-ch-ua": "\"Chromium\";v=\"94\", \"Google Chrome\";v=\"94\", \";Not A Brand\";v=\"99\"", "accept": "application/json", "sec-ch-ua-mobile": "?0", "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36", "sec-ch-ua-platform": "\"macOS\"", "sec-fetch-site": "same-origin", "sec-fetch-mode": "cors", "sec-fetch-dest": "empty", "referer": "http://127.0.0.1:8080/docs", "accept-encoding": "gzip, deflate, br", "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6", "cookie": "test_token=tokenABC" }, "cookies": { "test_token": "tokenABC" } }
可以看到 port 并不是 8080
request.url 对象
from fastapi import Query @app.get("/url/{item_id}") async def get_url(*, item_id: str, name: str = Query(...), request: Request): res = { # 请求 url "url": request.url, # 总的组成 "components": request.url.components, # 请求协议 "scheme": request.url.scheme, # 请求 host "hostname": request.url.hostname, # 请求端口 "url_port": request.url.port, # 请求 path "path": request.url.path, # 请求查询参数 "query": request.url.query, "fragment": request.url.fragment, "password": request.url.password } return res
请求结果
{ "url": { "_url": "http://127.0.0.1:8080/url/123?name=456", "_components": [ "http", "127.0.0.1:8080", "/url/123", "name=456", "" ] }, "components": [ "http", "127.0.0.1:8080", "/url/123", "name=456", "" ], "scheme": "http", "hostname": "127.0.0.1", "url_port": 8080, "path": "/url/123", "query": "name=456", "fragment": "", "password": null }
request.url 是一个对象(URL 类),得到的是一个字典
获取路径参数、查询参数
@app.get("/query_path/{item_id}") async def get_all(*, item_id: str, name: str = Query(...), request: Request): res = { # 获取路径参数 "path_params": request.path_params, "item_id": request.path_params.get("item_id"), # 获取查询参数 "query_params": request.query_params, "name": request.query_params["name"] } return res
请求结果
{ "path_params": { "item_id": "123" }, "item_id": "123", "query_params": { "name": "小菠萝" }, "name": "小菠萝" }
path_params、query_params返回的都是字典格式的数据
获取表单数据
@app.post("/form") async def get_form(*, username: str = Form(...), oassword: str = Form(...), request: Request): res = { # 获取表单数据 "form": await request.form() } return res