【问题标题】:How to get a POST request using python on HERE route matching API?如何在 HERE 路由匹配 API 上使用 python 获取 POST 请求?
【发布时间】:2019-11-12 17:51:34
【问题描述】:

我尝试使用 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


    【解决方案1】:

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

    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)
    

    【讨论】:

      【解决方案2】:

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

      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)
      

      【讨论】:

        猜你喜欢
        • 2019-11-25
        • 2022-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多