1

I'm quite new to apis and i have a problem. I want to use it to pass an image and process it. I thought just changing it to base64 will help and i will just pass a string to endpoint. But problem is that in base64 string there are '/' signs which break my url. Any ideas how i can fix it or there are any better ideas out there?

Code is simple:

app = FastAPI()


@app.get("/get_predictions/{base64_str}")
def get_predictions(base64_str: str):
    return get_model_predictions(base64_str)

get_model_predictions just handles the image and return what it has to return

0

2 Answers 2

2

you can use UploadFile to pass files to your endpoint.

Here is an example:

from fastapi import FastAPI, File, UploadFile

@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
    return {"filename": file.filename}

Be careful in my example I use the POST method and not GET. I allow myself to offer you a solution with POST since you say you want to 'pass' an image, so this is done with a POST route and not GET

You can learn more by looking at this section of the documentation: https://fastapi.tiangolo.com/tutorial/request-files/

Sign up to request clarification or add additional context in comments.

Comments

1

Using get request for file transfer is a bad idea. Firstly you will have a giant url, secondly information you pass in get request may be exposed to other people.

The right solution is to use post request that will hide all the information in the request body. There you can transfer your images not only by using base_64 but also like multipart-formdata.

Here is the documentation to the Fast API post requests: post request or answer how to post an image using Fast API

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.