【问题标题】:How to post JSON data to FastAPI, and retrieve whole JSON in the post function?如何将 JSON 数据发布到 FastAPI,并在 post 函数中检索整个 JSON?
【发布时间】:2022-09-27 19:25:31
【问题描述】:

我想将 JSON 对象传递给 FastAPI 后端。这是我在前端应用程序中所做的:

data = {\'labels\': labels, \'sequences\': sequences}
response = requests.post(api_url, data = data)

下面是 FastAPI 中后端 API 的样子:

@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)

    标签: python rest fastapi streamlit


    【解决方案1】:

    您应该改用json 参数(这会将标头中的Content-Type 更改为application/json),而不是用于发送表单数据的data。另外,请查看documentation,了解如何从对请求主体使用 Pydantic 模型中受益。

    response = requests.post(api_url, json=data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 2013-01-25
      • 1970-01-01
      • 2022-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多