I have my own simple API build with FastAPI. It's very simple -> /predict
endpoint expects image, and it returns some predictions from neural network. I connected it with JS front and it works very well.
Backend snippet:
@app.post("/predict")
async def create_upload_file(file: UploadFile):
image = await file.read()
result = my_net.predict(image)
return {"state": result}
Now I'm trying to write simple script.py with request package to upload image directly from my computer:
import requests
import json
url = 'https://MYAPI.com/predict'
file = open('images/image.png', 'rb')
response = requests.post(url, data=file)
print(response)
print(response.json())
It returns:
<Response [422]>
{'detail': [{'loc': ['body', 'file'], 'msg': 'field required', 'type': 'value_error.missing'}]}
I probably have to wrap the image with some information, but no idea how, I tried with 100 different configurations, I also tried to do it with Insomnia binary file request and get the same error 422.