litl / rauth

A Python library for OAuth 1.0/a, 2.0, and Ofly.
http://readthedocs.org/docs/rauth/en/latest/
MIT License
1.6k stars 174 forks source link

POST with file param not setting multipart/form-data? #166

Open tonyblank opened 10 years ago

tonyblank commented 10 years ago

I'm trying to make a multipart/form-data POST and can't figure out how to set the content-type to multipart/form-data. It's 'content-type': 'application/x-www-form-urlencoded'. - That's strange to me because I see the boundary in the body..

When I manually set the content-type header to multipart/form-data, then there's no boundary defined...

maxcountryman commented 9 years ago

Can you provide more context, how about a code sample that demonstrates the issue?

anssih commented 9 years ago

For e.g. POST, OAuth1Session.request() does req_kwargs['headers'].setdefault('Content-Type', FORM_URLENCODED).

This seems to override the proper "multipart/form-data" content type which PreparedRequest.prepare_body() gets from self._encode_files().

Overriding it manually using headers parameter for session.post() won't work since the content-type field needs to contain the boundary information which the rauth user doesn't know.

I workarounded this locally by making this modification:

         entity_method = method.upper() in ENTITY_METHODS
-        if entity_method:
+        if entity_method and not req_kwargs.get('files', None):
             req_kwargs['headers'].setdefault('Content-Type', FORM_URLENCODED)
maxcountryman commented 9 years ago

@anssih mind submitting a patch for that?

davidkhess commented 9 years ago

+1

This stumped me for a couple of hours when trying to upload to twitter's media/upload endpoint.

Same fix worked.

maxcountryman commented 9 years ago

@davidkhess patches welcome. :)

davidkhess commented 9 years ago

Ok, you guilted me into it. :-)