Skip to content

Instantly share code, notes, and snippets.

@shc261392
Created December 17, 2022 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shc261392/6bea0e723949d21cebfaa5a76616c72c to your computer and use it in GitHub Desktop.
Save shc261392/6bea0e723949d21cebfaa5a76616c72c to your computer and use it in GitHub Desktop.
A script to reproduce the issue that binary data is lost when using httpx client with authlib 1.2.0
from io import BytesIO
from authlib.integrations.requests_client import OAuth1Session
from authlib.integrations.httpx_client import OAuth1Client
CLIENT_ID = 'mockClientId'
CLIENT_SECRET = 'mockClientSecret'
ENDPOINT = 'http://localhost:8000'
requests_client = OAuth1Session(CLIENT_ID, CLIENT_SECRET)
httpx_client = OAuth1Client(CLIENT_ID, CLIENT_SECRET)
mock_file = BytesIO()
mock_file.write(b'foobar')
mock_file.seek(0)
print('Uploading file via authlib requests client')
r = requests_client.post(ENDPOINT, files={'file': mock_file})
mock_file.seek(0)
print('Uploading file via authlib httpx client')
g = httpx_client.post(ENDPOINT, files={'file': mock_file})
@shc261392
Copy link
Author

Here's a simple local HTTP server for debugging. Should be helpful for debugging/reproducing the issue.

@shc261392
Copy link
Author

requests client

Received header Content-Length:  146
Received actual bytes:  146
127.0.0.1 - - [17/Dec/2022 08:39:09] "POST / HTTP/1.1" 200 -

httpx client

Received header Content-Length:  0
Received actual bytes:  0
127.0.0.1 - - [17/Dec/2022 08:39:09] "POST / HTTP/1.1" 200 -

It's obvious the binary data are lost when using httpx client.

@shc261392
Copy link
Author

shc261392 commented Dec 17, 2022

The issue itself has nothing to do with OAuth-related implementations or OAuth tokens/signatures. Therefore no need to use any real OAuth client id/secret or implement any OAuth mechanism on server side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment