Select to view content in your preferred language

UpdateFeature REST API Python Script Unexpected Error

8336
2
Jump to solution
12-29-2014 08:53 PM
JosephMootz1
Regular Contributor

UpdateFeature REST API Python Script Unexpected Error
I am trying to update an ArcGIS Online hosted feature using a python script. I have tried a few examples of using Post methods combined with the Example Usage from the ArcGIS REST API documentation for UpdateFeature(http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#/Update_Features/02r3000000zt000000/) but in each case I get the following error:
{"error":{"code":500,"message":"An unexpected error occurred processing the request.","details":[]}}

I am using a json array of features that successfully updates the feature when manually submitted in the form at http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0....

Here is the code I use for the requests method:
   updateBaseURL = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0..."
    updateAttr = [

    {
      "attributes" : {
        "objectid": 12976095,
        "address" : "66TH ST and HARRISON ST"
      }
    }
    ]
    headers = {"Content-type": "application/json", "Accept": "text/plain"}
    r = requests.post(updateBaseURL, json=updateAttr,headers=headers)

Here is the code I use for the HTTPConnection method:
    updateAttr = [

    {
      "attributes" : {
        "objectid": 12976095,
        "address" : "66TH ST and HARRISON ST"
      }
    }
    ]
    headers = {"Content-type": "application/json", "Accept": "text/plain"}
    conn = httplib.HTTPConnection('sampleserver3.arcgisonline.com')
    conn.request("POST", "/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0/updateFeatures/?f=json", json.dumps(updateAttr), headers)

How Can I make this work?

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

I would think that something like this would work, but I'm getting a 400 error:

import urllib, urllib2, json

updateAttr = [{"attributes" : {"objectid": 12988724, "address" : "1217 2ND AVE some change"}}]
service_url = r"http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0"

params = urllib.urlencode({'f': 'json', 'features': updateAttr})
req = urllib2.Request("{0}/updateFeatures/?{1}".format(service_url, params))

response = urllib2.urlopen(req)
jsonResult = json.load(response)
print jsonResult

{u'error': {u'message': u'Unable to complete  operation.', u'code': 400, u'details': [u'Unable to update features']}}

The ObjectID in your example does not seem to exist...

View solution in original post

2 Replies
XanderBakker
Esri Esteemed Contributor

I would think that something like this would work, but I'm getting a 400 error:

import urllib, urllib2, json

updateAttr = [{"attributes" : {"objectid": 12988724, "address" : "1217 2ND AVE some change"}}]
service_url = r"http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0"

params = urllib.urlencode({'f': 'json', 'features': updateAttr})
req = urllib2.Request("{0}/updateFeatures/?{1}".format(service_url, params))

response = urllib2.urlopen(req)
jsonResult = json.load(response)
print jsonResult

{u'error': {u'message': u'Unable to complete  operation.', u'code': 400, u'details': [u'Unable to update features']}}

The ObjectID in your example does not seem to exist...

JosephMootz1
Regular Contributor

I got the following code to work based on your suggestion. Thanks so much. I think the key I was missing was formatting the parameters with "features"  

  1.    updateBaseURL = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/SanFrancisco/311Incidents/FeatureServer/0..."
  2. updateAttr = [{"attributes" : {"objectid": 12994837, "address" : "1217 2ND AVE some change"}}]
  3. params = urllib.urlencode({'f': 'json', 'features': updateAttr})
  4. updateURL = urllib2.Request(updateBaseURL,params)
  5. response = urllib2.urlopen(updateURL)
  6. jsonResult = json.load(response)
  7. print jsonResult

 

Thanks again.

0 Kudos