I have a simple fastapi endpoint where I want to receive a string value. In this case I tried it with a json body, but basically it doesn't need to be json. I really need only a simple string to separate the requests from each other. Unfortunately I can't access any of the request parameters with a GET method. I also tried POST method instead, but I get an error:
request:
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)
api:
@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
I can't print anything
with post or put:
@app.post("/ping/{key}")
async def get_trigger(key: Request):
...
or
@app.put("/ping/{key}")
async def get_trigger(key: Request):
I get an 405 Method not allowed
.
How can I get this fixed?
source https://stackoverflow.com/questions/71563503/cant-access-or-print-any-request-data-with-fastapi
Comments
Post a Comment