Reputation: 23
I'm trying to write a Python script to make an Amazon purchase using the Zinc API (see: https://zinc.io/docs/#full-api). I should be able to do this through a single POST request, so I read up about urllib and urllib2 here.
However, when I run my script, I receive this error:
{"_type":"error","code":"invalid_json","message":"The JSON in your request could not be parsed."}
I'm following Zinc's code pretty precisely, so I'm not sure why I'm getting the error.
Here is what I'm running (Note: my personal information is excluded, but included when I run the script):
import urllib
import urllib2
import json
url = 'https://api.zinc.io/v0/order'
values = {"client_token" : "public",
"retailer" : "amazon",
"products" :[{"product_id" : "0679753354", "quantity" : 1}],
"max_price" : 1700, "shipping_address" : {
"first_name" : "NAME",
"last_name" : "NAME",
"address_line1" : "ADDRESS",
"address_line2" : "",
"zip_code" : "ZIP",
"city" : "CITY",
"state" : "STATE",
"country" : "US"
},
"is_gift" : False,
"shipping_method" : "cheapest",
"payment_method" : {
"name_on_card" : "NAME",
"number" : "CARDNUMBER",
"security_code" : "SECCODE",
"expiration_month" : 1,
"expiration_year": 2015
},
"billing_address" : {
"first_name" : "NAME",
"last_name" : "NAME",
"address_line1" : "ADDRESS",
"address_line2" : "",
"zip_code" : "ZIP",
"city" : "CITY",
"state" : "STATE",
"country" : "US",
"phone_number" : "PHONE"
},
"retailer_credentials" : {
"email" : "EMAIL",
"password" : "PASSWORD"}
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
Any ideas on how to fix this JSON parsing issue would be much appreciated, as I haven't been able to figure out what's wrong.
Upvotes: 2
Views: 1270
Reputation: 146
You're url encoding your request, but not turning the Python data into JSON that the server can understand. Instead of calling urllib.urlencode(values), you should call json.dumps(values).
For example:
import urllib
import urllib2
import json
url = 'https://api.zinc.io/v0/order'
values = {"client_token" : "public",
"retailer" : "amazon",
"products" :[{"product_id" : "0679753354", "quantity" : 1}],
"max_price" : 1700, "shipping_address" : {
"first_name" : "NAME",
"last_name" : "NAME",
"address_line1" : "ADDRESS",
"address_line2" : "",
"zip_code" : "ZIP",
"city" : "CITY",
"state" : "STATE",
"country" : "US"
},
"is_gift" : False,
"shipping_method" : "cheapest",
"payment_method" : {
"name_on_card" : "NAME",
"number" : "CARDNUMBER",
"security_code" : "SECCODE",
"expiration_month" : 1,
"expiration_year": 2015
},
"billing_address" : {
"first_name" : "NAME",
"last_name" : "NAME",
"address_line1" : "ADDRESS",
"address_line2" : "",
"zip_code" : "ZIP",
"city" : "CITY",
"state" : "STATE",
"country" : "US",
"phone_number" : "PHONE"
},
"retailer_credentials" : {
"email" : "EMAIL",
"password" : "PASSWORD"}
}
req = urllib2.Request(url, json.dumps(values))
response = urllib2.urlopen(req)
the_page = response.read()
print the_page
Upvotes: 2
Reputation: 600051
You don't want to urlencode that data. That's for form values, not JSON.
In addition, you've imported the json
module, but you're not using it on your data. You should do it like this:
data = json.dumps(values)
req = urllib2.Request(url, data, {'content-type': 'application/json'})
Although it's much easier and better to use the third-party requests library for this sort of thing.
Upvotes: 2