Question: I keep getting 400 errors when trying to query the API with my code

Avatar image for kaleotter
KaleOtter

5

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#1  Edited By KaleOtter

Hey. Its my first time writing any API. I'm kind of a beginner-to-middling coder, but our college required we complete a unit on distributed systems and I'm the only one who can code in the group I was assigned.

So I'm writing something to query the GiantBomb for information about games using python, flask and urllib3. However, I keep getting 400 responses to queries I make. If i post the generated url string into postman or chrome, it works fine though. Can someone tell me what I'm doing wrong?

The Code in question

def apiConnection(self,remoteResourcePath,apiFields, filters):

http = urllib3.PoolManager()

print (remoteResourcePath)

#key to access the api

apiKey =[redacted]

#format incoming data as json, expand fields as nessecary.

#TODO: add additional logic if nessescary.

uri = 'http://www.giantbomb.com/api/'+ remoteResourcePath + '?api_key=%s&format=json' % (apiKey) \

+'&filter=%s' % (filters)\

+'&field_list=%s' %(apiFields)

print (uri)

try:

r= http.request(

'GET',

uri,

headers = {"User-Agent": 'Flask-Restful API'})

if r.status == 200: #we have valid data

returndata = {"status": True, "data": json.loads(r.data.decode('utf8'))}

else:

print (r.data)

returndata = {"status": False, "data": r.status}

except Exception as e:

returndata = {"status":False, "data":"There was a problem with urllib3 %s" % (e)}

return returndata

return returndata

The URLS It produces

http://www.giantbomb.com/api/games?api_key=[redacted]&format=json&filter=name:the matrix&field_list=name,original_release_date,original_game_rating,site_detail_url,image

Avatar image for igrat
igrat

31

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

I see you already have a user-agent set, so it's not that...

Have you tried changing the API url from HTTP to HTTPS? It redirects automatically, but maybe whatever library you're using is having trouble with that.

Another guess: Try URL-encoding your query variables (e.g. "the%20matrix" instead of "the matrix")