我正在尝试测试TomTom的API矩阵路由v2 ->同步矩阵(文档在这里)与Python的请求。
下面是我的代码,使用文档中的示例:import requests
apiKey = 'add_api_key_here'
apiURL = f'https://api.tomtom.com/routing/matrix/2?key={apiKey}'
post_params = {
"origins": [
{
"point": {"latitude": 36.98844, "longitude": -121.97482}
}
],
"destinations": [
{
"point": {"latitude": 34.13007, "longitude": -118.228610}
}
],
"options": {
"traffic": "historical",
"travelMode": "truck",
"vehicleCommercial": True
}
}
headers = {'Content-Type' : 'application/json' }
resp = requests.post(apiURL, data=post_params, headers=headers)
为什么我得到响应[400]错误,他说:
{"detailedError":{"code":"BAD_REQUEST","message":"Bad Request","details":[{"code":"MALFORMED_BODY","message":"Could not parse matrix request","target":"postBody"}]}}
我肯定错过了什么,只是我看不见而已。(
没关系,2分钟后我发现了自己的错误:用json=post_params替换request .post中的data=post_params。:)