输入应该是有效的字典或对象,以便从 FastAPI 请求失败时提取字段

问题描述 投票:0回答:1

我的FastAPI主要功能代码如下:

class Prompt(BaseModel):
    content: str = ""

@app.post("/query")
def query_item(prompt: Prompt = None):
   return promt

当我尝试使用curl发送我的帖子请求时

curl -X POST "http://localhost:8000/query"  -d '{"content":"Hello"}'
响应显示
Input should be a valid dictionary or object to extract fields from
需要帮助

我尝试使用请求包并在请求中发送显式字典,但问题仍然存在

curl fastapi pydantic
1个回答
0
投票

您需要告诉

curl
使用
application/json
作为内容类型:

λ curl -X POST "http://localhost:8111/query" -H "Content-Type: application/json" -d "{\"content\":\"Hello\"}"
{"content":"Hello"}

(并且您需要将

return promt
更改为
return prompt

© www.soinside.com 2019 - 2024. All rights reserved.