2

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.

1
  • Hi fnwtp, i hope you are doing well. I am trying to build an application using the ZINC api, I was curious how is it working for you so far? CYou mentioned that "I am following Zinc's code pretty precisely," from where? Could you please tell me the steps I need to follow I am not familiar with python. Thank you in advance! Commented Oct 9, 2014 at 22:00

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Daniel. This works perfectly. I'll definitely check out the requests library. Cheers
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.