Reputation: 445
I am trying to get a reference example working, but the API throws a 405 error:
import requests
import json
url = 'https://matrix.router.hereapi.com/v8/matrix'
headers = {'Content-Type': 'application/json'}
payload = {
"apiKey": <API_KEY>,
"origins": [
{"lat": 52.52103, "lng": 13.41268},
{"lat": 52.51628, "lng": 13.37771},
{"lat": 52.47342, "lng": 13.40357}
],
"regionDefinition": {
"type": "boundingBox",
"north": 52.53,
"south": 52.46,
"west": 13.35,
"east": 13.42
},
"matrixAttributes": ["distances"]
}
r = requests.get(url, headers=headers, params=payload)
print(json.dumps(r.json(), indent=2))
However, this gives
{"error":"Method not allowed for this action","error_description":"Method not allowed for this action"}
This obviously looks wrong, so I tried modifying my code to
r = requests.get(url, headers=headers, params=json.dumps(payload))
This also doesn't look right, so I tried the simplest example
import requests
import json
url = 'https://matrix.router.hereapi.com/v8/matrix?apiKey=API_KEY&async=false®ionDefinition=world&profile=truckFast'
headers = {'Content-Type': 'application/json'}
r = requests.get(url, headers=headers)
print(json.dumps(r.json(), indent=2))
This yields an r.url
of https://matrix.router.hereapi.com/v8/matrix?apiKey=API_KEY&async=false®ionDefinition=world&profile=truckFast
In all of these examples, the API still returns
{"error":"Method not allowed for this action","error_description":"Method not allowed for this action"}
Why is this happening?
In case it matters, my requests.__version__
is 2.27.1.
Upvotes: 0
Views: 476
Reputation: 445
Looks like the issue was indeed using GET rather than POST. However, posting a more complete answer with code examples, to resolve the questions around how to set the payload, etc.
import requests
import json
url = 'https://matrix.router.hereapi.com/v8/matrix?apiKey=<API_KEY>&async=false'
headers = {'Content-Type': 'application/json'}
payload = {
"origins": [
{"lat": 52.52103, "lng": 13.41268},
{"lat": 52.51628, "lng": 13.37771},
{"lat": 52.47342, "lng": 13.40357}
],
"regionDefinition": {
"type": "boundingBox",
"north": 52.53,
"south": 52.46,
"west": 13.35,
"east": 13.42
},
"matrixAttributes": ["distances"]
}
r = requests.post(url, headers=headers, data=json.dumps(payload))
print(json.dumps(r.json(), indent=2))
This results in
{
"matrixId": "fdaa07ea-f8d9-4b3f-a410-9c3b2d54e464",
"matrix": {
"numOrigins": 3,
"numDestinations": 3,
"distances": [
0,
2924,
7258,
3221,
0,
7710,
7333,
7721,
0
]
},
"regionDefinition": {
"type": "boundingBox",
"west": 13.35,
"south": 52.46,
"east": 13.42,
"north": 52.53
}
}
Upvotes: 0
Reputation:
Submit matrix for route calculation using POST method:
https://matrix.router.hereapi.com/v8/matrix?regionDefinition=world&profile=truckFast&apiKey=
Also please refer to Matrix routing API Examples
Upvotes: 1