I have made a fork of the Uber Application for Heroku
My Fork: https://github.com/CuriosityGym/Python-Sample-Application
I have modified the original code and the /price URL to get me estimates of price for a specific product ID, using the /requests/estimates endpoint documented here https://developer.uber.com/docs/riders/references/api/v1.2/requests-estimate-post.
@app.route('/price', methods=['GET'])
def price():
"""Example call to the price estimates endpoint.
Returns the time estimates from the given lat/lng given below.
"""
url = config.get('base_uber_url') + 'requests/estimate'
params = {
"product_id": "83941b0d-4be1-4979-a9c0-f0af5ee2b89b",
"start_latitude": config.get('start_latitude'),
"start_longitude": config.get('start_longitude'),
"end_latitude": config.get('end_latitude'),
"end_longitude": config.get('end_longitude')
}
print params
print generate_ride_headers(session.get('access_token'))
response = app.requests_session.post(
url,
headers=generate_ride_headers(session.get('access_token')),
data=params
)
return render_template(
'results.html',
endpoint='price',
data=response.text,
)
Here is the snippet of my code which uses the 1.2 version of the Uber Api. Other end points are working fine, its this one that does not work.
The print statements print to the Heroku logs and this is the output
{'product_id': '83941b0d-4be1-4979-a9c0-f0af5ee2b89b', 'end_longitude': '72.8811862', 'start_latitude': '18.936404', 'end_latitude': '19.0822507', 'start_longitude': '72.832546'}
{'Content-Type': 'application/json', 'Authorization': 'Bearer KA.eyJ2ZXJzaW9uIjkgsdshdJpZCI6IkNmcjAvRzhrUUNPaDNhSnRsUVZ6QlE9PSIsImV4cGlyZXNfYXQiOjE1MTAzMjA3NzgsInBpcGVsaW5lX2tleV9pZCI6Ik1RPT0iLCJwaXBlbGluZV9pZCI6MX0.JDoDTgaYJitK8Rtr35C6gTh5IQc7-P4T7mGg_wOYXu0'}
The error Reported by the api is
{"message":"Unable to parse JSON in request body.","code":"invalid_json"}
You need to encode your json as a string. Luckily requests can do this for you or you can use json.dumps()
to dump the object as a string.
Here are two examples:
Either do this:
import json
response = app.requests_session.post(
url,
headers=generate_ride_headers(session.get('access_token')),
data=json.dumps(params)
)
Or pass it as a kwarg json:
response = app.requests_session.post(
url,
headers=generate_ride_headers(session.get('access_token')),
json=params
)