Mapbox API PUT Datasets Feature return "Provide a single Feature to insert"

594 Views Asked by At

I am trying to add a feature to the Dataset via Mapbox API using Python. I'm following this instruction https://docs.mapbox.com/api/maps/#update-a-dataset but keep getting this error:

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

The code looks like this:

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)

I've tried the following but none of them work:

  • using requests instead of grequests
  • wrapping the dictionary with json.dumps()
  • changing the put parameter from data=dictionary to json=dictionary
  • Making sure the id for both data and URL are set to 1.

Postman of the exact same request does not have the error. What am I missing?

2

There are 2 best solutions below

2
Moritz On

Given a dataset with dataset ID dataset exists, your request body looks ok. Please add the header

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

Also can you check if you meet these specs:

This should be one individual GeoJSON feature, not a GeoJSON FeatureCollection. If the GeoJSON feature has a top-level id property, it must match the feature_id you use in the URL endpoint.

0
voratima On

It turns out I forgot the header. Thanks to Mortiz for pointing that out. After the update I got

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

That's because I need to wrap the dictionary inside json.dumps(). Then the error became

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

That's because the id in the dictionary has to be a string i.e. "id":"1" not "id":1. Here's the code that works:

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)