Select to view content in your preferred language

Using the POST Method for Batch Geocoding with ArcGIS Server?

1501
1
06-11-2019 12:41 PM
StaceMaples
Occasional Contributor III

I'm trying to hit my geocoding servers REST API:[https://locator.stanford.edu/arcgis/rest/services/geocode/USA_StreetAddress/GeocodeServer] (ArcGIS Server 10.6.1)

using the POST method (which, BTW, could use an example or two, there only seems to be this VERY brief "note" on WHEN to use POST, not HOW: https://developers.arcgis.com/rest/geocode/api-reference/geocoding-geocode-addresses.htm#ESRI_SECTIO...).

I'm trying to use the requests.post, and I think I've managed to get the token accepted, etc..., but I keep getting:

{'error': {'code': 400, 'message': 'Unable to complete operation.', 'details': []}}

Which, based on my experience with using the GET method, means something is wrong with the way I am formatting the submitted data? Though, I've been cutting and pasting directly from the Esri Help file examples for GET, but I'm not sure that's what is needed for POST. Any thoughts on how to proceed are welcome. I'm about to see what adding headers might do?

Here's my code:

# import the requests library
import requests

# Multiple address records
addresses={ 
 "records": [
 {
 "attributes": {
 "OBJECTID": 1,
 "Street": "380 New York St.",
 "City": "Redlands",
 "Region": "CA",
 "ZIP": "92373"
 }
 },
 {
 "attributes": {
 "OBJECTID": 2,
 "Street": "1 World Way",
 "City": "Los Angeles",
 "Region": "CA",
 "ZIP": "90045"
 }
 }
 ]
}

# Parameters
# Geocoder endpoint
URL = 'https://locator.stanford.edu/arcgis/rest/services/geocode/USA_StreetAddress/GeocodeServer/geocodeAddresses?'
# token from locator.stanford.edu/arcgis/tokens
mytoken = <GeneratedToken>
# output spatial reference id 
outsrid = 4326
# output format
format = 'pjson'
# params data to be sent to api 
params ={'outSR':outsrid,'f':format,'token':mytoken}

# Use POST to batch geocode
r = requests.post(url=URL, data=addresses, params=params)

print(r.json())
print(r.text)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I've altered the attribute names for my service, as seen here:

Address Fields:
  • Street ( type: esriFieldTypeString , alias: Street or Intersection , required: false , length: 100 )
  • City ( type: esriFieldTypeString , alias: City or Placename , required: false , length: 40 )
  • State ( type: esriFieldTypeString , alias: State , required: false , length: 100 )
  • ZIP ( type: esriFieldTypeString , alias: ZIP Code , required: false , length: 10 )
0 Kudos
1 Reply
J_R_Matchett
New Contributor III

I was having the same issue and this post on GIS Stack Exchange led me to the solution. The post data needs to be passed as a dictionary with an 'addresses' key and the records converted to a JSON string as its value. Using your example:

import json
r = requests.post(url=URL, params=params, data={'addresses': json.dumps(addresses)})
0 Kudos