rauth icon indicating copy to clipboard operation
rauth copied to clipboard

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

Open tonyblank opened this issue 11 years ago • 6 comments

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...

tonyblank avatar Aug 14 '14 10:08 tonyblank

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

maxcountryman avatar Dec 17 '14 00:12 maxcountryman

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)

anssih avatar Dec 28 '14 00:12 anssih

@anssih mind submitting a patch for that?

maxcountryman avatar Dec 31 '14 16:12 maxcountryman

+1

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

Same fix worked.

davidkhess avatar Apr 03 '15 14:04 davidkhess

@davidkhess patches welcome. :)

maxcountryman avatar Apr 03 '15 14:04 maxcountryman

Ok, you guilted me into it. :-)

davidkhess avatar Apr 03 '15 15:04 davidkhess