Fastapi: POST response base64

Created on 2 Dec 2020  路  3Comments  路  Source: tiangolo/fastapi

Example

class Base64(BaseModel):
    base64_str: str
    photo_name: str

@app.post("/base64")
async def post_base64Image(b64: Base64):
    imgstr = b64.base64_str
    imgdata = base64.b64decode(imgstr)
    filename = '%s.png' % b64.photo_name 
    with open(filename, 'wb') as f:
        f.write(imgdata)
    photo_id = 0
    paths = int(photo_id / 1000) * 1000
    with open(r"E:\images1024x1024\%05d\%05d.png" % (paths, photo_id), "rb") as img_file:
        b64_output = base64.b64encode(img_file.read())
    return {"Image" : b64_output.decode('utf-8')}



@app.post("/response_model/", response_model=Base64)
async def response_models(b64: Base64):
    photo_id = 0
    paths = int(photo_id / 1000) * 1000
    with open(r"E:\images1024x1024\%05d\%05d.png" % (paths, photo_id), "rb") as img_file:
        b64_output = base64.b64encode(img_file.read())
    b64.base64_str = b64_output.decode('utf-8')
    return b64

Description

  • If I use this two HTTP POST to send base64 image with size 64*64 is ok.
  • But when I want to send 1024*1024 it just doesn't work.
  • The client side is using swift to call the POST method.
  • How could I send 1024*1024 base64 to it?

  • Or is my POST funtion is not well defined?

    Environment

  • macOS

  • FastAPI Version: 0.61.1
  • Python version: 3.8.6
question

Most helpful comment

If you can provide us an example that doesn't depend on your local path, some image samples and explain how we can reproduce the same behavior, it will be very cool. :sunglasses: :+1:

All 3 comments

What error are you getting in your server console?

Not show any error, but just show nothing.

If you can provide us an example that doesn't depend on your local path, some image samples and explain how we can reproduce the same behavior, it will be very cool. :sunglasses: :+1:

Was this page helpful?
0 / 5 - 0 ratings