1

The help file at https://support.esri.com/en/technical-article/000012383 shows how to (manually) update a layer's definition in ArcGIS Online, in order to increase its maxRecordCount.

How can I perform this operation programatically, via Python?

I tried the following unsuccessfully:

import requests

url = "https://services3.arcgis.com/XXXuniqueId/ArcGIS/admin/services/XXXlayerName.FeatureServer/0/updateDefinition?token=XXXtoken"

params = {
  'editingInfo': {"lastEditDate": ''},
  'maxRecordCount': 98765
}

data = {'updateDefinition':  params, "f": "json"}
response = requests.post(url, data=data)
print(response.text)

The response from ArcGIS Online is the error: {"error":{"code":400,"message":"Unable to update feature service layer definition.","details":["Invalid JSON primitive: editingInfo."]}}

I suspect the issue is with the nested JSON so I also (unsuccessfully) tried variations on this:

param1 = {"lastEditDate": ""}
params = {
  'editingInfo': json.dumps(param1),
  'maxRecordCount': 98765
}
data = {'updateDefinition':  params, "f": "json"}

I was able to submit the request via Postman by including the parameters as form-data, making me hopeful that the issue is with the Python syntax:

enter image description here

What is the correct approach for updating the layer definition via Python?

1

I think you need to use the UpdateDefinition URL parameter and a JSON object.

Try this link for more information

https://developers.arcgis.com/rest/services-reference/online/update-definition-feature-service-.htm

Also the format of parameters being pass in the post() method need to be changed, such that what is being passed in this method in correct URL formatting. This is why the postman web page works, but your method doesn't.

Here for more info: https://www.urlencoder.io/learn/

As per your comments, try to format the URL first and encode the data then use it as the parameter in the post method. Ensure it includes the '?' as well as the json object. That way, you have a better structured URL and the encoding will ensure the correct URL formatting.

url += "?" + urllib.parse.urlencode(data)
6
  • Thanks for the tip. I edited the question to show the revised URL, including the token, and the JSON sent. And I examined the response from the POST request and it's basically the original page sent back, not the "success" response. Interesting it doesn't include either the success: true or error responses mentioned in the help file. Any further clues? Jul 21 at 10:27
  • Looking at the Postman response, that suggests its not an access issue - however just to confirm, try to use 'status' outside of Potman.... - ie" services3.arcgis.com/XXXuniqueId/ArcGIS/admin/services/… (plus token).... in URL.... this will confirm access. Also check this line 'response = requests.post(url, data=data)' --- data=data? is that correct?
    – nr_aus
    Jul 22 at 0:19
  • it works in Postman whether I include ?token=X in the URL, or whether I put the token into the form-data as a parameter. I'm not an expert in Python requests but it seems that you need to use the data parameter to send a POST request. My variable happens to be called data but it could easily be data=blah Jul 22 at 0:25
  • 1
    Your final comment something like URL/UpdateDefinition?params&token gave me the answer - encode the parameters and include them in the URL rather than as a data object: url += "?" + urllib.parse.urlencode(data). Thanks! Do you want to edit your answer and I'll accept it? Jul 22 at 0:59
  • 1
    great news - have updated it now to include the new info and include some references for URL encoding for information by future users as well as your comments above.
    – nr_aus
    Jul 22 at 2:17

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.