如何通过HERE路由匹配API使用python获取POST请求?

问题描述 投票:0回答:2

我尝试使用python的请求库发出POST请求,该请求库如下所示:

url = "https://rme.api.here.com/2/matchroute.json?routemode=car&filetype=CSV&app_id={id}&app_code={code}" response = requests.post(url,data='Datasets/rtHereTest.csv')

我得到的响应代码是400

{'faultCode': '16a6f70f-1fa3-4b57-9ef3-a0a440f8a42e',
 'responseCode': '400 Bad Request',
 'message': 'Column LATITUDE missing'}

但是,在我的数据集中,here我具有HERE API文档中要求进行调用的所有标题。

是否有我做错的事情,我不太了解POST调用或要求,因为HERE文档未明确给出许多示例。

python-3.x python-requests here-api
2个回答
0
投票

发布请求的数据字段应包含实际数据,而不仅仅是文件名。尝试先加载文件:

f = open('Datasets/rtHereTest.csv', 'r')
url = "https://rme.api.here.com/2/matchroute.json?routemode=car&filetype=CSV&app_id={id}&app_code={code}"
response = requests.post(url, data=f.read())
f.close()

这是我在自己的代码中使用的,其坐标之前已定义:

query = 'https://rme.api.here.com/2/matchroute.json?routemode=car&app_id={id}&app_code={code}'.format(id=app_id, code=app_code)
coord_strings = ['{:.5f},{:.5f}'.format(coord[0], coord[1]) for coord in coords]
data = 'latitude,longitude\n' + '\n'.join(coord_strings)
result = requests.post(query, data=data)

0
投票

您可以尝试使用以下格式发布数据。

import requests
    url = "https://rme.api.here.com/2/matchroute.json"
    querystring = {"routemode":"car","app_id":"{app_id}","app_code":"{app_code}","filetype":"CSV"}
    data=open('path of CSV file','r')
    headers = {'Content-Type': "Content-Type: text/csv;charset=utf-8",
        'Accept-Encoding': "gzip, deflate",
        }
    response = requests.request("POST", url, data=data.read().encode('utf-8'), params=querystring,headers=headers)
    print(response.status_code)

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.