HERE 矩阵路由 API - "Method not allowed for this action"
HERE Matrix Routing API - "Method not allowed for this action"
我正在尝试让 reference example 工作,但是 API 抛出 405 错误:
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))
然而,这给出了
{"error":"Method not allowed for this action","error_description":"Method not allowed for this action"}
这显然看起来不对,所以我尝试将我的代码修改为
r = requests.get(url, headers=headers, params=json.dumps(payload))
的 r.url
这个也不对,我试了最简单的例子
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))
的 r.url
在所有这些示例中,API 仍然是 returns
{"error":"Method not allowed for this action","error_description":"Method not allowed for this action"}
为什么会这样?
以防万一,我的 requests.__version__
是 2.27.1.
使用POST方法提交路由计算矩阵:
https://matrix.router.hereapi.com/v8/matrix?regionDefinition=world&profile=truckFast&apiKey=
另请参考矩阵路由API示例
看起来问题确实是使用 GET 而不是 POST。但是,通过代码示例发布更完整的答案,以解决有关如何设置有效负载等问题。
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))
这导致
{
"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
}
}
我正在尝试让 reference example 工作,但是 API 抛出 405 错误:
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))
然而,这给出了
{"error":"Method not allowed for this action","error_description":"Method not allowed for this action"}
这显然看起来不对,所以我尝试将我的代码修改为
r = requests.get(url, headers=headers, params=json.dumps(payload))
的 r.url
这个也不对,我试了最简单的例子
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))
的 r.url
在所有这些示例中,API 仍然是 returns
{"error":"Method not allowed for this action","error_description":"Method not allowed for this action"}
为什么会这样?
以防万一,我的 requests.__version__
是 2.27.1.
使用POST方法提交路由计算矩阵:
https://matrix.router.hereapi.com/v8/matrix?regionDefinition=world&profile=truckFast&apiKey=
另请参考矩阵路由API示例
看起来问题确实是使用 GET 而不是 POST。但是,通过代码示例发布更完整的答案,以解决有关如何设置有效负载等问题。
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))
这导致
{
"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
}
}