0

I'm performing a batch download from my AGOL account, and using the ESRI documentation export item I can export content to shapefiles. I would like to then download the newly created shapefiles. In order to accomplish this I have to be able to have the ID number of the newly created shapefile. According to the documentation it is in the JSON response.

sudo code:

import requests
import arcrest
import json

exporturl = self.root_url +'/users/' + self.username + '/export'
export = requests.post(exporturl, params=FsParams)
export_response = export.json()

What I have tried:

response = export_response['exportItemId']
print response

Error: KeyError: 'exportItemId'

printing response = export_response returns the params from the post

Using the requests library can I access the JSON response? If this is not possible with requests what method will work?

4
  • 1
    Could you provide an example of what export_response and export prints out? Based on your code (looking at it, not trying it), if your URL is correct you should be getting a response.However, a guess might be your syntax of export.json() is wrong, it should be export_respone =json.loads(export) (but just a guess as I stopped using requests
    – KHibma
    Commented Dec 29, 2015 at 16:36
  • I know that it is not json.loads() as that does not work. I modified the the OP with the error
    – risail
    Commented Dec 29, 2015 at 16:44
  • 1
    Error is good: its saying the response you got back doesn't have that value in it. do a print export_response and see all the key:values. This will tell you either your initial request is wrong, or hopefully something useful to help you debug in the returned response
    – KHibma
    Commented Dec 29, 2015 at 16:48
  • @KHibma your suggestion of printing export_response exposed an issue with my first two layers. response = export_response['exportItemId'] worked once I put it in a try clause. If you move that to an answer Ill accept it.
    – risail
    Commented Dec 29, 2015 at 16:59

1 Answer 1

2

The code, as posted, is correct when working with requests and json. As indicated from the comments, when making a request over multiple items, you might get a different response back depending on what the original item was requested against.

The error in question, KeyError: 'exportItemId' comes from the fact the response returned a dictionary without the exportItemId key and you tried to access it. There are multiple ways to handle this. A simple try / except as you indicate will allow you to pass over instances when you dont get back a key which you were expecting. Alternatively you could do an explicit check that the key exists in the dictionary before attempting to access it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.