Search code examples
pythonflaskmultipart

Return multipart/formdata containing png file and markdown as a flask response


Hi I am trying to return multipart from data from a get request. However I am unable to return a MultipartEncoder object as a response. I have attempted the solution at Can I serve a multipart http response in Flask?

The below code only returns the 'toPersonEmail' field. How does one return m as a response in Flask?

@app.route("/multipart", methods=['GET'])
def send_multipart():
     m = MultipartEncoder(fields={ 'markdown': "> Block quotes are written like so in markdown.",
                                   'toPersonEmail':'d@d.com',
                                   'files': ("pnggrad16rgb.png", open("pnggrad16rgb.png", 'rb'),
                                   'image.png')})

     
     return Response(m.fields, mimetype=m.content_type)

Solution

  • Flask has no specific provision for multipart mime responses; multipart/form is a mimetype normally only used in a POST request to the server, not in a response from a HTTP server.

    If you must return valid multipart mime data, render the body to bytes and set the right headers on a response object; the `(

    m = MultipartEncoder(fields={ 'markdown': "> Block quotes are written like so in markdown.",
                                   'toPersonEmail':'d@d.com',
                                   'files': ("pnggrad16rgb.png", open("pnggrad16rgb.png", 'rb'),
                                   'image.png')})
    
    return (m.to_string(), {'Content-Type': m.content_type})
    

    If you wanted to stream the response, you'd have to supply your own generator function that reads from the multipart encoder in chunks:

    def chunked_reader(f, chunksize=2 ** 20):  # 1Mb chunks
        while True:
            chunk = f.read(chunksize)
            if not chunk:
                return
            yield chunk
    

    and use that to wrap your MultipartEncoder() instance when streaming response data:

    # include the Content-Length header, even though we are streaming
    return Response(
        chunked_reader(m), content_type=m.content_type,
        headers={'Content-Length': m.len})
    

    The MultipartEncoder() object supports a .read() method just like files do.