Reputation: 137
I have a fast api application, where I want to have a post request.
from pydantic import BaseModel
from fastapi import FastAPI
class Data(BaseModel):
num: str
app = FastAPI()
@app.post("/set/")
def set_param(data: Data):
return data
When I send request from another file:
import requests,json
req = requests.post('http://0.0.0.0:5000/set/',data={'num':'1'}).json()
I have an error:
INFO: 127.0.0.1:54236 - "POST /set/ HTTP/1.1" 422 Unprocessable Entity
I don;t know what should I change to eliminate this error.
Upvotes: 1
Views: 4332
Reputation: 20756
You are not sending a valid JSON.
You should use json
instead of data
.
json={"num":"1"}
Use it like this
req = requests.post('http://0.0.0.0:5000/set/',json={'num':'1'}).json()
Or you can use json.dumps()
req = requests.post('http://0.0.0.0:5000/set/', json.dumps(data={'num':'1'})).json()
Upvotes: 3