Adafruit_requests library functions differently than Python requests library

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
will3790
 
Posts: 3
Joined: Wed Jul 27, 2022 6:47 pm

Adafruit_requests library functions differently than Python requests library

Post by will3790 »

So I'm trying to run a post request to a TD Ameritrade API on an adafruit Magtag device using the adafruit_requests module. I have run the same code in Python using the requests module, which has proven to work fine.

My Magtag device has successfully connected to the internet and executed the test requests adafruit supplied, but will not work with my code and returns a "duplicate header" error. Below is the complete code that I ran on my MagTag device as well as the error message that was returned.

Code: Select all

import ssl
import wifi
import socketpool
import adafruit_requests
import json

# Get wifi details from a secrets.py file
from secrets import secrets

# Connect to wifi
wifi.radio.connect(secrets["ssid"],secrets["password"])
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

# Get json data
refreshToken = ''
client_id = ''
with open("TDtokens.json","r") as tokens_file, open("TDsecrets.json","r") as secrets_file:
    refreshToken = json.load(tokens_file)['refresh_token']
    client_id = json.load(secrets_file)['client_id']

# Refresh token needs to be triggered every 30 minutes, as authToken expires.
url = r"https://api.tdameritrade.com/v1/oauth2/token"
headers = {'Content-Type':'application/x-www-form-urlencoded'}
data = {'grant_type': 'refresh_token','refresh_token': refreshToken,'client_id':client_id}
authReply = requests.post(url=url, headers=headers, data=data)
print(authReply.json())

Code: Select all

{'fault': {'faultstring': 'Duplicate Header "Content-Type"', 'detail': {'errorcode': 'protocol.http.DuplicateHeader'}}}
I have done a lot of tweaking and I can't figure out where this error is coming from, whether from adafruit or TD Ameritrade's side. Upon reading the documentation for the adafruit_requests module I do not see any reason why an error would be raised for this type of request.

If you have any experience with Adafruit requests or Python requests any advice would be greatly appreciated.

User avatar
tannewt
 
Posts: 3298
Joined: Thu Oct 06, 2016 8:48 pm

Re: Adafruit_requests library functions differently than Python requests library

Post by tannewt »

The reply you are seeing looks like it is coming from the Ameritrade API. This looks like a bug in circuitpython requests because it always adds content-type internally based on data passed in. https://github.com/adafruit/Adafruit_Ci ... ts.py#L591 It should work if you omit the headers argument.

User avatar
will3790
 
Posts: 3
Joined: Wed Jul 27, 2022 6:47 pm

Re: Adafruit_requests library functions differently than Python requests library

Post by will3790 »

tannewt wrote: Tue Nov 15, 2022 6:11 pm The reply you are seeing looks like it is coming from the Ameritrade API. This looks like a bug in circuitpython requests because it always adds content-type internally based on data passed in. https://github.com/adafruit/Adafruit_Ci ... ts.py#L591 It should work if you omit the headers argument.
Thank you so much for the reply! That makes sense now that you point it out. I have removed the headers argument and it still does not work, it returns {'error': 'invalid_grant'}. I have also tried removing the code you highlighted in the library while keeping the headers argument and it still continues to return the duplicate headers error. I believe this may be an issue with the callback URL I have set w/ Ameritrade, it is currently set to http://localhost and works well on my machine.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”