I’m trying to send an image file from one api route to another.
I get no response on sending an image file.
Code :
@app.post("/send_image/")
async def send_image(image_file: UploadFile = File(...)):
try:
# Read the image file as bytes
image_bytes = await image_file.read()
# Specify the URL of the destination API
destination_url = "http://127.0.0.1:8000/model_pred/upload/"
response = requests.post(destination_url, files={'image_file' : image_bytes})
# Check the response status code
if response.status_code == 200:
return JSONResponse(content={"status": "Image sent successfully"})
else:
return JSONResponse(content={"status": "Failed to send image"}, status_code=500)
except Exception as e:
return JSONResponse(content={"status": f"An error occurred: {str(e)}"}, status_code=500)
# Route to receive image
@app.post("/receive_image/")
async def receive_image(image_file: UploadFile = File(...)):
try:
return JSONResponse(content={"filename": image_file.filename, "content_type": image_file.content_type})
except Exception as e:
return JSONResponse(content={"status": f"An error occurred: {str(e)}"}, status_code=500)
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)
Curl script : curl -X POST -F “[email protected]” http://127.0.0.1:8000/send_image/
Can someone please help with any suggestions?