Python API not working

Hi, I am trying to fetch API response.
It’s not coming:
Code:

import requests
import json
lat = 17.5901322
lng = 78.491586
parameters = {
    "point": str(lat) + "%2C" + str(lng) + "&point=" + "17.3850442%2C78.486671",
    "locale": "en-us",
    "elevation": "true",
    "profile": "car",
    "use_miles": "false",
    "selected_detail": "Elevation",
    "layer": "Omniscale",
    "key": "I won't keep the key here"
}
response = requests.get("https://graphhopper.com/api/1/route", params=parameters)
def jprint(obj):
    text = json.dumps(obj, sort_keys=True, indent=4)
    print(text)

jprint(response.json())

Resopnse:

{
    "message": "Specify at least 2 points"
}

When I send through the Browser:

(https://graphhopper.com/api/1/route?point=17.590132%2C78.491586&point=17.385044%2C78.486671&locale=en-us&elevation=true&profile=foot&use_miles=false&selected_detail=Elevation&layer=Omniscale&key=i won't give here)

Here I am getting the response

Let requests encode the params for you instead of building the string by hand:

parameters = {
    "point": [f"{lat},{lng}", "17.3850442,78.486671"],
    "locale": "en-us",
    "elevation": "true",
    "profile": "car",
    "use_miles": "false",
    "selected_detail": "Elevation",
    "layer": "Omniscale",
    "key": "I won't keep the key here"
}

BTW for a question related to a specific service or tool, it would be better to be raised on StackOverflow or the bug tracker of that tool.

1 Like

Thanks a lot it worked like a charm!