POST + file + auth

Hi,

I'm trying to send a post request with a file which requires an authentication...

Following the docs I do:

requests.post(url, files={"file" : open("filename")}, auth=my_auth_object)

Unfortunately I'm getting this error:

  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 383, in open
    response = self._open(req, data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 401, in _open
    '_open', req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 361, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 1130, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 1102, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 874, in request
    self._send_request(method, url, body, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 914, in _send_request
    self.send(body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 719, in send
    self.sock.sendall(str)
  File "/opt/virtualenvs/brainaetic/lib/python2.6/site-packages/eventlet/greenio.py", line 287, in sendall
    tail = self.send(data, flags)
  File "/opt/virtualenvs/brainaetic/lib/python2.6/site-packages/eventlet/greenio.py", line 265, in send
    len_data = len(data)
AttributeError: multipart_yielder instance has no attribute '__len__'

Ideas? 

P.S: I'f I remove the auth the request doesn't fail but obviously the auth is required in my case.


Looks like the issue is actually being caused by evenlet, which I enable if it's installed. 

In the next release, the library won't monkeypatch automatically, which will fix this issue. I hope to push that out this evening, or tomorrow.

In the meantime, either uninstall eventlet from your virtualenv or take out these lines (https://github.com/kennethreitz/requests/blob/master/requests/core.py#L19-30)


Hi, 

thanks for the quick answer...

I did that, doing so raises this exception:

  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 383, in open
    response = self._open(req, data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 401, in _open
    '_open', req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 361, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 1130, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib2.py", line 1102, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 874, in request
    self._send_request(method, url, body, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 914, in _send_request
    self.send(body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/httplib.py", line 719, in send
    self.sock.sendall(str)
  File "<string>", line 1, in sendall
TypeError: sendall() argument 1 must be string or buffer, not instance

Where str in self.sock.sendall(str) is an instance of multipart_yielder which is being passed for some reason. I've noticed that using auth, the _StreamingHTTPMixin is not called I might be wrong though...


Hmm, thanks for letting me know. You're using the Python 2.6 that comes with Snow Leopard?


Yeap, I have a small workaround for this (just waiting for your commit). If you haven't fixed this I can fork and do the pull request.


I haven't fixed it yet. If you have, feel free to send a pull request. :)


Merged!