-
-
Notifications
You must be signed in to change notification settings - Fork 421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't upload binary files in Python2 because body is converted to unicode #258
Comments
Hrm, this seems problematic. Is it possible for you to provide a minimal reproducer that uses httpbin.org? |
I'm not sure how to use httpbin with oauth2, but upon further investigation this seems to happen specifically when using prepared requests, here's a snippet that reproduces the error from io import BytesIO
from oauthlib.oauth2 import LegacyApplicationClient
from requests_oauthlib import OAuth2Session, OAuth2
from requests import Request, Session
username = 'myuser'
password = 'mypassword'
client_id = 'myclientid'
client_secret = 'myclientsecret'
client = LegacyApplicationClient(client_id=client_id)
session = OAuth2Session(client=client)
token = session.fetch_token(
token_url='https://httpbin.org/post',
username=username,
password=password,
client_id=client_id,
client_secret=client_secret)
req = Request(
'post',
'https://httpbin.org/post',
data={'name': 'TestFile'},
files={'file': BytesIO('\x89')},
auth=OAuth2(client=client))
prep = session.prepare_request(req)
res = session.send(prep) |
So, I don't think the OAuth2Session supports the prepared request flow: you have to use the |
Been adapting an old project so I can't speak for why the original used prepared requests. Adding an OAuth2 instance to a regular requests library Session object causes the same issue though so it appears to be a problem with the OAuth2 class rather than the OAuth2Session class. |
Hi @rainyday, it's been over a year since there was any activity on this issue. Is this still a problem for you? If not, I'm going to close the issue. |
What would be the reason for closing this? Do you know at least if this is still reproducible? |
Appears somewhat related to this issue: https://github.com/kennethreitz/requests/issues/1252
We're trying to send a multipart/form-data request that includes a text field and a binary file. All our code uses regular strings but it appears that requests_oauthlib converts the message body to a unicode string and when httplib tries to append the binary data it gives the following error:
The text was updated successfully, but these errors were encountered: