1

I am trying to update metadata (XML file) for ArcGIS online items using the REST API but I am not quite sure how to do that.

It says in the documentation that I should be able to change the metadata on the Add Item and Update Item operations, but not exactly how:

http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#//02r30000009v000000

Here is my try which returns a success but doesn't actually change the metadata.

def update_metadata(user, item, token, metadata):

    d = {'metadata': (metadata, open(metadata, 'rb')),
         "token" :token,
         "f":"json"}

    url = 'http://www.arcgis.com/sharing/rest/content/users/{0}/items/{1}/update'.format(user, item)
    r = requests.post(url, data = d)

    response = json.loads(r.content)

    if 'error' in response.keys():
        raise Exception(response['message'], response['details'])

    return response

Did anyone already successfully updated metadata for ArcGIS online items using the REST API, and if yes how?

1

Typically what I do when mimicking something from ArcGIS Online is do the same thing in ArcGIS Online while looking at the network traffic through Google Developer Tools/Fiddler. In the case of metadata, the following is the request made when saved:

Request through Google Developer Tools

I'm not entirely how this can be replicated through Python but it might give you a better idea of what is required.

  • 1
    Thanks, very helpful. After playing around a bit more I got it to work. I posted it as an answer. – Thomas Feb 14 '16 at 23:15
1

Figured it out in the end. In the request form, I have to keep file and other payload data separate. The function below worked for me:

def update_metadata(user, item, token, metadata):

    d = {"overwrite": "true",
         "token": token,
         "f": "json"}
    f = {'metadata': ('metadata.xml', open(metadata, 'rb'), 'text/xml', {'Expires': '0'})} 

    url = 'http://www.arcgis.com/sharing/rest/content/users/{0}/items/{1}/update'.format(user, item)
    r = requests.post(url, data = d, files = f)

    response = json.loads(r.content)

    if 'error' in response.keys():
        raise Exception(response['message'], response['details'])

    return response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.