python requests api not posting in-memory zipped file
<p><em>question</em>: How can I get this to work</p>

<p>I'm trying to use the python requests api to send a zipped file to a server. I saw this method in the docs:</p>

<pre><code>r = requests.post(url, files=open('foo.png', 'rb'))
</code></pre>

<p>but the difference between what I'm doing, is that the zipped file that I have is in memory, there's just a python object, no physical zipped version of the file is created:</p>

<p>I'm using the zipfile api, and this is how I'm creating my zip file:</p>

<pre><code>inMemoryOutputFile = StringIO()
outFile = zipfile.ZipFile(inMemoryOutputFile, "w",
        compression=zipfile.ZIP_DEFLATED)
</code></pre>

<p>and trying the following (after writing to the zip file):</p>

<pre><code>r = requests.post(url, outFile)
</code></pre>

<p>however its not working, looks like the object is not being recognized as a parameter. here's the stack trace</p>

<pre><code>Traceback (most recent call last):   File
"/Users/abdulahmad/Desktop/upload-script-ve/bin/cogs", line 11, in
&lt;module&gt;
    sys.exit(main())   File "/Users/abdulahmad/Desktop/upload-script-ve/lib/python2.7/site-packages/cogs/run.py",
line 396, in main
    return run(sys.argv)   File "/Users/abdulahmad/Desktop/upload-script-ve/lib/python2.7/site-packages/cogs/run.py",
line 384, in run
    return instance()   File "/Users/abdulahmad/Desktop/upload-script-ve//src/ctl.py",
line 53, in __call__
    handler = uploader(self.url, self.file)   File "/Users/abdulahmad/Desktop/upload-script-ve//src/uploader.py",
line 24, in __call__
    response = self.session.post(url, files=payload) 

#this is where I'm adding the file (the payload)

File "/Users/abdulahmad/Desktop/upload-script-ve/lib/python2.7/site-packages/requests/sessions.py",
line 511, in post
    return self.request('POST', url, data=data, json=json, **kwargs)   File
"/Users/abdulahmad/Desktop/upload-script-ve/lib/python2.7/site-packages/requests/sessions.py",
line 454, in request
    prep = self.prepare_request(req)   File "/Users/abdulahmad/Desktop/upload-script-ve/lib/python2.7/site-packages/requests/sessions.py",
line 388, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),   File "/Users/abdulahmad/Desktop/upload-script-ve/lib/python2.7/site-packages/requests/models.py",
line 296, in prepare
    self.prepare_body(data, files, json)   File "/Users/abdulahmad/Desktop/upload-script-ve/lib/python2.7/site-packages/requests/models.py",
line 447, in prepare_body
    (body, content_type) = self._encode_files(files, data)   File "/Users/abdulahmad/Desktop/upload-script-ve/lib/python2.7/site-packages/requests/models.py",
line 150, in _encode_files
    fdata = fp.read() TypeError: read() takes at least 2 arguments (1 given)
</code></pre>

<p>actual code:</p>

<pre><code>inMemoryOutputFile = StringIO()

    parentDir, dirToZip = os.path.split(dirPath)
    def trimPath(path):
        archivePath = path.replace(parentDir, "", 1)
        if parentDir:
            archivePath = archivePath.replace(os.path.sep, "", 1)
        if not includeDirInZip:
            archivePath = archivePath.replace(dirToZip + os.path.sep, "", 1)
        return os.path.normcase(archivePath)

    outFile = zipfile.ZipFile(inMemoryOutputFile, "w",
        compression=zipfile.ZIP_DEFLATED)

    for (archiveDirPath, dirNames, fileNames) in os.walk(dirPath):
        for fileName in fileNames:
            filePath = os.path.join(archiveDirPath, fileName)
            outFile.write(filePath, trimPath(filePath))
        if not fileNames and not dirNames:
            zipInfo = zipfile.ZipInfo(trimPath(archiveDirPath) + "/")

            outFile.writestr(zipInfo, "")
    outFile.close()
    return outFile
</code></pre>