There are a number of Python modules that you can use to make REST requests. There are really too many! Modules include urllib2
, httplib2
, pycurl
, and requests
. requests
is definitely the best of the bunch in my opinion. It is cleaner and easier to use for repeated interaction with RESTful APIs. Once you've made the request, you can then parse the JSON response with the Python json
module. In this recipe, you will learn how to do this.
The Python requests
module can be used to submit requests
to an ArcGIS Server resource and process the returned response. Follow these steps to learn the basic steps involved in submitting requests
and processing the response using the requests
module:
Before we get started, make sure that you have downloaded and installed the requests
module, using pip
. If you haven't already done so, I have provided the following instructions to install both pip
and the requests
module:
This recipe and all subsequent recipes in this chapter use the Python requests
module. If you don't already have this module installed on your computer, you will need to do so at this time. The requests
module is installed using pip
, which needs to be installed on your computer before the requests
module is installed. Later versions of Python including 2.7.9 (on the Python2 series) and Python 3.4 include pip
by default, so you may have it already. To test to see if you already have pip
installed, you can enter the following from a DOS prompt:
pip install requests
If you don't have pip
installed, you'll get an error message and you'll need to install pip
(https://pip.pypa.io/en/latest/installing.html). Once installed, you can download and install the requests
module by using the preceding install command.
C:ArcpyBookCh12ReqJSON.py
.requests
and json
modules:import requests import json
pjson
, a JSON format contained in a pretty format to provide easy readability:import requests
import json
agisurl = "http://server.arcgisonline.com/arcgis/rest/services?f=pjson"
get()
method in the requests
module to submit an HTTP request to the ArcGIS REST API. You'll store the response in a variable called r
:import requests
import json
agisurl = "http://server.arcgisonline.com/arcgis/rest/services?f=pjson"
r = requests.get(agisurl)
import requests
import json
agisurl = "http://server.arcgisonline.com/arcgis/rest/services?f=pjson"
r = requests.get(agisurl)
print(r.text)
json.loads()
method to parse the returned json
into a Python dictionary object. Go ahead and remove the previous print statement as well:import requests import json agisurl = "http://server.arcgisonline.com/arcgis/rest/services?f=pjson" r = requests.get(agisurl) decoded = json.loads(r.text) print(decoded)
C:ArcpyBookcodeCh12ReqJSON.py
solution file.loads()
method has converted the json
output into a Python dictionary:{u'folders': [u'Canvas', u'Demographics', u'Elevation', u'Ocean', u'Reference', u'Specialty', u'Utilities'], u'services': [{u'type': u'MapServer', u'name': u'ESRI_Imagery_World_2D'}, {u'type': u'MapServer', u'name': u'ESRI_StreetMap_World_2D'}, {u'type': u'GlobeServer', u'name': u'I3_Imagery_Prime_World'}, {u'type': u'GlobeServer', u'name': u'NASA_CloudCover_World'}, {u'type': u'MapServer', u'name': u'NatGeo_World_Map'}, {u'type': u'MapServer', u'name': u'NGS_Topo_US_2D'}, {u'type': u'MapServer', u'name': u'Ocean_Basemap'}, {u'type': u'MapServer', u'name': u'USA_Topo_Maps'}, {u'type': u'MapServer', u'name': u'World_Imagery'}, {u'type': u'MapServer', u'name': u'World_Physical_Map'}, {u'type': u'MapServer', u'name': u'World_Shaded_Relief'}, {u'type': u'MapServer', u'name': u'World_Street_Map'}, {u'type': u'MapServer', u'name': u'World_Terrain_Base'}, {u'type': u'MapServer', u'name': u'World_Topo_Map'}], u'currentVersion': 10.2}
In this simple recipe, you learned how to use the Python requests
module to submit a request to an ArcGIS Server instance by using the requests.get()
method, and then process the response from the server. The json.loads()
method was used to convert the response to a Python dictionary object for easier processing. The response contains basic data about the ArcGIS Server instance, including folders, services, and versions. We'll look at more complex examples in the coming recipes.