【问题标题】:Mapbox API PUT Datasets Feature return "Provide a single Feature to insert"Mapbox API PUT 数据集功能返回“提供要插入的单个功能”
【发布时间】:2020-11-17 04:27:27
【问题描述】:

我正在尝试使用 Python 通过 Mapbox API 向数据集添加功能。我正在遵循此指令https://docs.mapbox.com/api/maps/#update-a-dataset,但不断收到此错误:

{'message': 'Provide a single Feature to insert'}

代码如下所示:

rs = []
dictionary = {
"id":1,
"type":"Feature",
"properties":{},
"geometry":{"coordinates":[-83.750246, 42.269375],"type":"Point"}}
url = "https://api.mapbox.com/datasets/v1/voratima/"+dataset+"/features/1?access_token="+access_token
rs.append(grequests.put(url, data=dictionary, hooks = {'response' : do_something}))
grequests.map(rs, exception_handler=exception_handler)

我尝试了以下方法,但都没有效果:

  • 使用requests 而不是grequests
  • json.dumps()包装字典
  • 将 put 参数从 data=dictionary 更改为 json=dictionary
  • 确保将数据和 URL 的 id 设置为 1

完全相同请求的邮递员没有错误。我错过了什么?

【问题讨论】:

    标签: python python-requests mapbox mapbox-marker grequests


    【解决方案1】:

    假设数据集 ID 为 dataset 的数据集存在,您的请求正文看起来没问题。 请添加标题

    headers = {'Content-type': 'application/json'}
    
    

    您还可以检查您是否符合这些规格:

    这应该是一个单独的 GeoJSON 功能,而不是 GeoJSON 特征集合。如果 GeoJSON 功能具有顶级 id 属性, 它必须与您在 URL 端点中使用的 feature_id 匹配。

    【讨论】:

    • 两者的 feature_id 都是 1。字典是 GeoJSON 功能。添加标题会给我一个不同的错误。 {'message': 'Unexpected token i'}
    • 字典中的id必须是字符串。
    【解决方案2】:

    原来我忘记了标题。感谢 Mortiz 指出这一点。更新后我得到了

    <Response [400]> {'message': 'Unexpected token i'} 
    

    那是因为我需要将字典包装在 json.dumps() 中。然后错误变成了

    <Response [422]> {'message': 'Request URI does not match feature id'}
    

    这是因为字典中的id 必须是字符串,即"id":"1" 而不是"id":1。这是有效的代码:

    rs = []
    dictionary = {
        "id":"1",
        "type":"Feature",
        "properties":{},
        "geometry":{"coordinates":[-83.750246, 42.269375],"type":"Point"}}
    headers = {'Content-type': 'application/json'} 
    url = "https://api.mapbox.com/datasets/v1/voratima/"+dataset+"/features/1?access_token="+access_token
    rs.append(grequests.put(url, data=json.dumps(dictionary), headers=headers, hooks = {'response' : do_something}))
    grequests.map(rs, exception_handler=exception_handler)
    

    【讨论】:

      猜你喜欢
      • 2021-08-10
      • 2016-04-11
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      相关资源
      最近更新 更多