pythonhttp-headerspython-requestsmultipartform-datarauth

Sending multipart data with rauth / requests


I am developing an API-Client for transferring real estates. The interface provided is using Oauth1 and multipart HTTP posts in order to upload attachment data. The service provider wants to have attachments uploaded in a certain format as described here: http://api.immobilienscout24.de/our-apis/import-export/attachments/post.html (section Example for picture) What I basically need to do is to create a MIME multipart post for

  1. An XML document
  2. A binary file

So far, I tried to use the file argument of rauth, resp. requests to deliver both, the XML and the binary file. But I cannot figure out how to add the different MIME types (e.g. application/xml and image/jpeg) to the respective multipart sections. How can I do that?


Solution

  • Like this:

    import requests
    
    files = {
        'attachment': ('filename.jpg',
               open('path/to/filename.jpg', 'rb'),
               'image/jpeg; name=filename.jpg',
               {'Content-Transfer-Encoding': 'binary'}),
        'metadata': ('body.xml',
               open('/path/to/body.xml', 'rb'),
               'application/xml; name=body.xml',
               {'Content-Transfer-Encoding': 'binary'})}
    
    response = requests.post(url, files=files)