【发布时间】:2022-10-08 16:30:53
【问题描述】:
我有一个简单的 FastAPI 端点,我想在其中接收一个字符串值。在这种情况下,我尝试使用 JSON 正文,但基本上它不需要是 JSON。我真的只需要一个简单的字符串来将请求彼此分开。不幸的是,我无法使用GET 方法访问任何请求参数。我也尝试了POST 方法,但出现错误:
要求:
url = "http://127.0.0.1:5000/ping/"
payload=json.dumps({"key":"test"})
headers = {
"Content-Type": "application/json"
}
response = requests.request("POST", url, headers=headers, json=payload)
print(response.text)
接口:
@app.get("/ping/{key}")
async def get_trigger(key: Request):
key = key.json()
test = json.loads(key)
print(test)
test2 = await key.json()
print(key)
print(test2)
return
我无法使用post 或put 打印任何内容:
@app.post("/ping/{key}")
async def get_trigger(key: Request):
...
or
@app.put("/ping/{key}")
async def get_trigger(key: Request):
我收到405 Method not allowed 错误。
我怎样才能解决这个问题?
【问题讨论】: