Search code examples
pythonpython-requestspng

How do I convert a .png file to a multi-part file?


I have a .png file that I am pulling from a server, and I want to post that file to another server using an API which calls for a multi-part file as its parameter. Are .png files already considered multi-part, or do I need to manipulate the response data in some way?


Solution

  • Assuming you are talking about the HTML multipart form: multipart is the format of the HTTP request body. It has nothing to do with the file itself. You need to build a request like

    POST /test.html HTTP/1.1 
    Host: target.org 
    Content-Type: multipart/form-data;boundary="ABCD" 
    
    --ABCD
    Content-Disposition: form-data; name="my_file"; filename="example.txt" 
    
    <<put the actual binary data of the file here>>
    

    You can build it manually, but the libraries you are using would probably provide some easier way (you don't say what you are using, so it's impossible to tell)