Skip to content
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

Fix duplicate header issue #149

Merged
merged 2 commits into from
Jan 5, 2024

Conversation

justmobilize
Copy link
Collaborator

@justmobilize justmobilize commented Dec 28, 2023

When helping a member in the discord channel #help-with-circuitpython, I had made the mistake of thinking that this library didn't automatically deal with turning a dict into a urlencoded string. I had them convert it by hand and try and it worked.

As it turns out - it does, but also sends the header Content-Type: application/x-www-form-urlencoded, which some systems (Spotify as one) can't handle getting the header twice.

This would fix issue #148

Test:

from os import getenv
import adafruit_requests
import socketpool
import ssl
import wifi

wifi_ssid = getenv("CIRCUITPY_WIFI_SSID")
wifi_password = getenv("CIRCUITPY_WIFI_PASSWORD")
client_id = getenv("SPOTIFY_CLIENT_ID")
client_secret = getenv("SPOTIFY_CLIENT_SECRET")

ssl_context=ssl.create_default_context()
pool = socketpool.SocketPool(wifi.radio)
wifi.radio.connect(wifi_ssid, wifi_password)

requests = adafruit_requests.Session(pool, ssl_context)

data = {"grant_type": "client_credentials", "client_id": client_id, "client_secret": client_secret}
headers = {"Content-Type": "application/x-www-form-urlencoded"}
url = "https://accounts.spotify.com/api/token"
r = requests.post(url, data=data, headers=headers)
print(r.status_code)
print(r.text)

Using the main branch of Adafruit_CircuitPython_Requests, this will return a 400 because it sends Content-Type twice, using this branch will succeed

@justmobilize justmobilize marked this pull request as ready for review December 28, 2023 19:07
@justmobilize
Copy link
Collaborator Author

This may also resolve: #40

Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi - several suggestions.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Thanks for the fix @justmobilize!

I confirmed the duplicated Content-Type header issue with a simple http server and wireshark to view the raw TCP data stream. I tried a few other things before resorting to wireshark, but flask and a few of the other basic server examples I tried made it difficult for me to see the raw request. Many of them treat headers as dictionary-like so it seems to resolve duplicates without much trouble.

I confirmed the fix does successfully get rid of that duplicated header.

All testing was was done on a QTPy ESP32-S2 8.2.9

@justmobilize
Copy link
Collaborator Author

This looks good to me. Thanks for the fix @justmobilize!

I confirmed the duplicated Content-Type header issue with a simple http server and wireshark to view the raw TCP data stream. I tried a few other things before resorting to wireshark, but flask and a few of the other basic server examples I tried made it difficult for me to see the raw request. Many of them treat headers as dictionary-like so it seems to resolve duplicates without much trouble.

I confirmed the fix does successfully get rid of that duplicated header.

All testing was was done on a QTPy ESP32-S2 8.2.9

Thanks for testing! And happy to do this (and I hope a bunch more)

@FoamyGuy
Copy link
Contributor

FoamyGuy commented Jan 5, 2024

Looks like all requested changes have been addressed. Merging this one. Thanks again for all of your work on this @justmobilize!

@FoamyGuy FoamyGuy merged commit 178dbc3 into adafruit:main Jan 5, 2024
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 6, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_Requests to 2.0.4 from 2.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_Requests#149 from justmobilize/fix-duplicate-header-issue

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
@justmobilize justmobilize deleted the fix-duplicate-header-issue branch January 23, 2024 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants