0

Currently I have a simple Flask server that once the user hits the /endpoint it renders an HTML form where she can select an image for upload. Upon clicking on "submit", the server receives the image as a POST request, performs some processing on it and returns a JSON with the resulting analysis, for instance:

{
"status": "ok",
"coins found": 10
}

Everything works fine from the browser. I even managed to use Postman to bypass the form-filling phase and send the POST request directly to the server with an image attached and got back the expected response.

In the next phase I want to see how changing my image-processing pipeline is going to affect the server throughput. For that I intend to use Vegeta, and now I need to have my payload as a text file so I can tell Vegeta how to create the requests to perform its attacks.

What I really want to know is the following: "How do I store a jpg Image in a plain text file so vegeta can pick it up and the Flask server decode it properly".

For now I've tried reading the image, encoding the byte stream to base64 and storing it in the file, for instance:

------xx
Content-Disposition: form-data; name="image"; filename="sweden-swedish-flag-national-cycling-jersey.jpg"
Content-Type: image/jpeg


/9j/4AAQSkZJRgABAQEASABIAAD...
------xx--

However the Flask app fails to open the image with the following error: OSError: cannot identify image file <_io.BytesIO object at 0x12c17bc50>.

For completeness, this is the code that I'm using to open images and is working fine for requests from the browser and Postman:

    import io
    from PIL import Image

    bytestream = io.BytesIO(request.files['image'])
    image = Image.open(bytestream)
lfelix
  • 121
  • 4
  • check below link it may help you. [https://stackoverflow.com/questions/29104107/upload-image-using-post-form-data-in-python-requests][1] – Sukesh Hublikar Sep 17 '19 at 04:56
  • Kind of. In this case the OP wants to submit a request using Python code. In my case I want to dump the HTTP request to a file so I can use another utility to send the request. I've tried to dump the request generated by the `request` module, but unsuccessfully. – lfelix Sep 17 '19 at 12:52

0 Answers0