JamesPoole JamesPoole - 6 months ago 19
JSON Question

Retrieving JSON Data Using Flask on Python

I have a URL that I have to send a GET request (I think) to retrieve JSON data.

My question is simply how do I go about doing this?

(This is the example URL that I am using in the code to retrieve JSON from)
https://api.outpan.com/v2/products/5060144218476apikey=xxxxxxxx

This is how I've gone about it so far but I know that it is far from right.
Any help would be much appreciated.

apiKey = "xxxxxxxxxxxxxxx"
barcode = "yyyyyyyyyyyyy"

@app.route("/https://api.outpan.com/v2/products/" + barcode + "?apikey=" + apiKey, methods=["GET"])
def getData():
data = request.json
dataDict = json.loads(data)

return dataDict

Answer

Use requests, like so:

import requests

params = {
    "apikey" : "899..." # Omitted for security
}
barcode = "5060144218476"

response = requests.get(
    "https://api.outpan.com/v2/products/" + barcode, 
    params = params)
print response.json()