1

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.

Chris
  • 18,724
  • 6
  • 46
  • 80
rumcajs
  • 129
  • 1
  • 10
  • 1
    Related answers can be found [here](https://stackoverflow.com/a/70641755/17865804) and [here](https://stackoverflow.com/a/71176578/17865804), as well as [here](https://stackoverflow.com/a/70657469/17865804) and [here](https://stackoverflow.com/a/70711176/17865804). – Chris Feb 13 '23 at 11:51
  • 1
    Yes.. It was my oversight 'file' instead of 'files' . I flaged it as duplicate and linked to already answered. Thanks :) – rumcajs Feb 13 '23 at 12:17

0 Answers0