I am sending and image and also json data to my API in the following way:
import requests
filename = "test_image.jpeg"
files = {'my_file': (filename, open(filename, 'rb'))}
json={'first': "Hello", 'second': "World"}
response = requests.post('http://127.0.0.1:8000/file', files=files, params=json)
How do I receive both the image and json data on the server-side via FastAPI?
My code looks like this:
@app.post('/file')
def _file_upload(my_file: UploadFile = File(...), params: str = Form(...)):
image_bytes = my_file.file.read()
decoded = cv2.imdecode(np.frombuffer(image_bytes, np.uint8), -1)
pg_image = cv2.resize(decoded, (220, 220))
return {"file_size": params}
However, this gives me the following error:
<Response [422]>
{'detail': [{'loc': ['body', 'params'], 'msg': 'field required', 'type': 'value_error.missing'}]}
Is there something which I am doing wrong here?