Ok, so I used the code above along with the requests module and got it almost all working (probably don't have to use both urllib and requests, but it works). Works fine, except for one thing: the REST data is in WGS_1984_Web_Mercator_Auxiliary_Sphere, and I want to import it into WGS 1984. From what I've read, setting outSR to 4326 should do it, but no luck. Still imports in Web Mercator. If I go to the rest endpoint in a browser and set it, works correctly, just not in the script. Is this a bug, am I setting it wrong, ??? Thanks.(Sorry, forgotten how to format as code).
def getJson(token):
# query parameters
query_dict = {"token" : token["token"], "where" : "1=1", "outfields" : "*", "outSR" : "4326", "f" : "json"}
baseurl = "http://company/arcgis/rest/services/Maps/city/MapServer/"
# layers in REST endpoints are identified by index number, not name
layerid = layer_dict[infeature]
query = "/query"
urltoquery = baseurl + layerid + query
r = requests.get(urltoquery, auth=(username, password), params=query_dict)
# build json file name, saved to user desktop
currentuser = os.getenv('username')
ext = ".json"
filenm = os.path.join(r"C:\Users", currentuser, r"Desktop\project" + infeature) + ext
with open(filenm, "wb") as f:
for line in r.iter_content():
f.write(line)
try:
arcpy.JSONToFeatures_conversion(filenm, outfeature)
except Exception as e:
err = "\nError importing json file\n{}".format(e)
arcpy.AddError(err)