Making HTTP requests and parsing the response with Python

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.

Getting ready

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:

How to do it…

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:

Note

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.

  1. Open IDLE (or another Python development environment) and select File | New Window. Save the file under the name C:ArcpyBookCh12ReqJSON.py.
  2. Import the requests and json modules:
    import requests
    import json
  3. Create a new variable that contains a URL to a list of services provided by ArcGIS Online. This URL contains an output format of 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"
    
  4. Use the 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)
    
  5. Now, let's print out the response that is returned:
    import requests
    import json
    agisurl = "http://server.arcgisonline.com/arcgis/rest/services?f=pjson"
    r = requests.get(agisurl)
    print(r.text)
    
  6. Save your script.
  7. Run your script and you will see the following output. This is the JSON response that has been returned by the ArcGIS REST API:
    How to do it…
  8. Use the 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)
    
  9. You can check your work by examining the C:ArcpyBookcodeCh12ReqJSON.py solution file.
  10. Save and run your script to see the output. The 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}
    

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset