Skip to content

Instantly share code, notes, and snippets.

@jgravois
Last active April 22, 2022 18:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgravois/d4a49a7ba38d49d3c42a21706cf69d79 to your computer and use it in GitHub Desktop.
Save jgravois/d4a49a7ba38d49d3c42a21706cf69d79 to your computer and use it in GitHub Desktop.
what's required to use python requests to POST to AGOL/ArcGIS Server?
import requests
import json
agol_url = r'http://services1.arcgis.com/uRIm5IkWjDXybgFb/arcgis/rest/services/LA_Nhood_Change/FeatureServer/0/query'
server_url = r'http://sampleserver6.arcgisonline.com/arcgis/rest/services/EmergencyFacilities/FeatureServer/0/query'
# i tried several different headers, to no avail
# headers={"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
# headers = {"Content-Type": "application/x-www-form-urlencoded", "Accept":"/*/", "Accept-Encoding":"gzip, deflate", "Accept-Language":"en-US,en;q=0.8,ar;q=0.6"}
headers={"content-type":"application/json","Accept":"application/json"}
rGet = requests.get(agol_url, {"f":"json","where":"1=1","returnCountOnly":"true"}, headers=headers)
print 'python get request returns JSON, as expected'
print rGet.text
rPost = requests.post(agol_url, data=json.dumps({"f":"json","where":"1=1","returnCountOnly":"true"}), headers=headers)
print 'python post request returns html, indicating that parameters are ignored'
print rPost.text
# shoot, i cant even figure out how to POST with cURL
# curl --data "f=json&where=1=1&returnCountOnly=true" http://services1.arcgis.com/uRIm5IkWjDXybgFb/arcgis/rest/services/LA_Nhood_Change/FeatureServer/0/query
# curl -H "Content-Type: application/json; charset=UTF-8" -X POST -d '{\"f\":\"json\"}' http://services1.arcgis.com/uRIm5IkWjDXybgFb/arcgis/rest/services/LA_Nhood_Change/FeatureServer/0/query
@jgravois
Copy link
Author

jgravois commented Jun 11, 2019

two years later, I finally figured it out: https://stackoverflow.com/a/56552964/3019940

@williamsalex
Copy link

Thanks for posting this John, ArcGIS is awesome but their API can be a nightmare sometimes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment