![](/img/trans.png)
[英]How to add both file and JSON body in a FastAPI POST request?
[英]How to post request JSON to fastapi, and retrieve whole JSON in the post function
我想将 JSON object 传递给 fastapi 后端。 这是我在前端应用程序中所做的:
data = {'labels': labels, 'sequences': sequences}
response = requests.post(api_url, data = data)
这是后端 API 在 fastapi 中的样子:
@app.post("/api/zero-shot/")
async def Zero_Shot_Classification(request: Request):
data = await request.json()
但是,我看到一个错误:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
您应该改用json
参数(这会将 header 中的Content-Type
更改为application/json
),而不是用于发送表单data
的数据。 另外,请查看有关如何从使用 Pydantic 模型作为请求主体中受益的文档。
response = requests.post(api_url, json=data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.