Reputation: 61
So I'm trying to learn python so I can work with an api. The tutorial I was using was using python 2 and urllib. I'm running python 3.6, so it wasn't working. So I decided to try and learn about requests. I'm having a little bit of trouble converting from urllib to requests.
import requests
import json
parameters = {"apikey": "mykey", "queries": "SN74S74N"}
response = requests.get("http://octopart.com/api/v3/parts/match", params =
parameters)
data = response.json()
#print(type(data))
print(data)
The error I get when I run this is
{'message': 'JSON decode error: SN74S74N', '__class__': 'ClientErrorResponse'}
I'm not sure why I'm getting this error. But I think it might be because my parameters aren't set up right. Is requests capable of doing the same thing they have in the documentation? https://octopart.com/api/docs/v3/rest-api#endpoints-parts-match
Sorry this is vague, I just started learning python and apis. Will be around to further clarify any questions.
Upvotes: 0
Views: 108
Reputation: 109
From looking at the docs you provided for the API it looks like your parameters aren't structured how the API requires it.
Under the examples section, it shows the queries
sent with the request as:
queries = [
{'mpn': 'SN74S74N',
'reference': 'line1'},
{'sku': '67K1122',
'reference': 'line2'}
]
So for your example you need:
queries = [
{'mpn': 'SN74S74N',
'reference': reference goes here}
]
and use the request as you have in your code.
Upvotes: 1