Skip to content

Instantly share code, notes, and snippets.

@csmoore
Created October 24, 2017 17:57
Show Gist options
  • Save csmoore/452f12f8d6f76617c7877a72e88c3b4d to your computer and use it in GitHub Desktop.
Save csmoore/452f12f8d6f76617c7877a72e88c3b4d to your computer and use it in GitHub Desktop.
Python sample GET/POST calls to ArcGIS REST Service
#------------------------------------------------------------------------------
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#------------------------------------------------------------------------------
# Filename: ArcGISRestSamples.py
# Description: Makes a REST GET/POST calls to ArcGIS REST
# Notes: **Requires Python 2 to run (urllib2)**
#------------------------------------------------------------------------------
import urllib
# Note: will fail here if running with Python 3:
import urllib2
debug = True
service = 'https://sample.com/server/rest/services/SampleFeatureService/FeatureServer'
layers = ['0', '1', '2']
operation = 'query'
# Ex: other operations: 'deleteFeatures', 'calculate'
def SampleRestGet():
# Records that have "created_date" set (is not null)
query = 'where="created_date"+is+not+null'
response_format = 'f=pjson'
for layer in layers:
url = service + '/' + layer + '/' + operation + '?' + query + '&' + response_format
# create the request and set headers
rest_request = urllib2.Request(url)
rest_request.add_header('Accept', 'application/json')
rest_request.add_header("Content-type", "application/x-www-form-urlencoded")
# make the request
if debug:
print 'Making Request:'
print url
rest_response = urllib2.urlopen(rest_request)
if debug:
print '---------'
print 'Response:'
print rest_response.read()
def SampleRestPost():
# Records that have "created_date" set (i.e. it is not null)
query = '"created_date" is not null'
response_format = 'pjson'
values = { 'where': query, \
'f': response_format }
data = urllib.urlencode(values)
for layer in layers:
url = service + '/' + layer + '/' + operation
# create the POST request and set headers
rest_request = urllib2.Request(url, data) # Note: POST request has data parameter
rest_request.add_header('Accept', 'application/json')
rest_request.add_header("Content-type", "application/x-www-form-urlencoded")
# make the request
if debug:
print 'Making Request:'
print url
rest_response = urllib2.urlopen(rest_request)
if debug:
print '---------'
print 'Response:'
print rest_response.read()
if __name__ == '__main__':
SampleRestPost()
SampleRestGet()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment