post - QPX Express API from Python -
i trying utilize google's qpx express api python. maintain running pair of issues in sending request. @ first tried this:
url = "https://www.googleapis.com/qpxexpress/v1/trips/search?key=my_key_here" values = {"request": {"passengers": {"kind": "qpxexpress#passengercounts", "adultcount": 1}, "slice": [{"kind": "qpxexpress#sliceinput", "origin": "rdu", "destination": location, "date": datego}]}} info = json.dumps(values) req = urllib2.request(url, data, {'content-type': 'application/json'}) f = urllib2.urlopen(req) response = f.read() f.close() print(response)
based upon code from: urllib2 , json
when run above code next error message:
typeerror: post info should bytes or iterable of bytes. cannot of type str.
i searched solution , adapted code based upon next question: typeerror: post info should bytes or iterable of bytes. cannot str
i changed code this:
url = "https://www.googleapis.com/qpxexpress/v1/trips/search?key=aizasycmp2znki3j91sog7a7m7-hzcn402fyuzo" values = {"request": {"passengers": {"kind": "qpxexpress#passengercounts", "adultcount": 1}, "slice": [{"kind": "qpxexpress#sliceinput", "origin": "rdu", "destination": location, "date": datego}]}} info = json.dumps(values) info = data.encode("utf-8") req = urllib2.request(url, data, {'content-type': 'application/json'}) f = urllib2.urlopen(req) response = f.read() f.close() print(response)
however, when run code next error message:
urllib.error.httperror: http error 400: bad request
i tried changing utf-8 ascii unsuccessful. how can working properly?
here solution using excelent requests library.
import json import requests api_key = "your api key here" url = "https://www.googleapis.com/qpxexpress/v1/trips/search?key=" + api_key headers = {'content-type': 'application/json'} params = { "request": { "slice": [ { "origin": "txl", "destination": "lim", "date": "2015-01-19" } ], "passengers": { "adultcount": 1 }, "solutions": 2, "refundable": false } } response = requests.post(url, data=json.dumps(params), headers=headers) info = response.json() print info
i not sure why request not working. maybe request parameters wrong. date needs in future!
python post python-3.x google-api urllib
No comments:
Post a Comment