Upload a file to a HTTP endpoint in POST

Finally I found a solution, don’t know if it’s THE solution but couldn’t find another.
It looks like the only way is actually parsing the Multipart MIME content contained in request.body but, after a looooong googling session, that worked out to be simpler than expected.

In server code I need to import

from requests_toolbelt.multipart import decoder

and then it’s as simple as:

    multipart_bytes = request.body.get_bytes()
    content_type = request.body.content_type
    if len(decoder.MultipartDecoder(multipart_bytes, content_type).parts) > 0:
        uploaded_file = decoder.MultipartDecoder(multipart_bytes, content_type).parts[0]

uploaded_file is a standard bytes array object you can use to build a BlobMedia and then it’s Anvil’s world.

BR

4 Likes