繁体   English   中英

如何将请求 JSON 发布到 fastapi,并在帖子 function 中检索整个 JSON

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM