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_SECTION1_351DE4FD98FE44958C8194EC5A7BEF7D).
I'm trying to use the requests.post, and I think I've managed to get the token accepted, etc..., but I keep getting:
<SPAN class="punctuation token">{</SPAN><SPAN class="string token">'error'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">'code'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">400</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'message'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Unable to complete operation.'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'details'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>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:
<SPAN class="comment token"># import the requests library</SPAN>
<SPAN class="keyword token">import</SPAN> requests
<SPAN class="comment token"># Multiple address records</SPAN>
addresses<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"records"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"attributes"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"OBJECTID"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"Street"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"380 New York St."</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"City"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Redlands"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"Region"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"CA"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"ZIP"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"92373"</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"attributes"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="string token">"OBJECTID"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"Street"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"1 World Way"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"City"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Los Angeles"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"Region"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"CA"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">"ZIP"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"90045"</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="comment token"># Parameters</SPAN>
<SPAN class="comment token"># Geocoder endpoint</SPAN>
URL <SPAN class="operator token">=</SPAN> <SPAN class="string token">'https://locator.stanford.edu/arcgis/rest/services/geocode/USA_StreetAddress/GeocodeServer/geocodeAddresses?'</SPAN>
<SPAN class="comment token"># token from locator.stanford.edu/arcgis/tokens</SPAN>
mytoken <SPAN class="operator token">=</SPAN> <SPAN class="operator token"><</SPAN>GeneratedToken<SPAN class="operator token">></SPAN>
<SPAN class="comment token"># output spatial reference id </SPAN>
outsrid <SPAN class="operator token">=</SPAN> <SPAN class="number token">4326</SPAN>
<SPAN class="comment token"># output format</SPAN>
format <SPAN class="operator token">=</SPAN> <SPAN class="string token">'pjson'</SPAN>
<SPAN class="comment token"># params data to be sent to api </SPAN>
params <SPAN class="operator token">=</SPAN><SPAN class="punctuation token">{</SPAN><SPAN class="string token">'outSR'</SPAN><SPAN class="punctuation token">:</SPAN>outsrid<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'f'</SPAN><SPAN class="punctuation token">:</SPAN>format<SPAN class="punctuation token">,</SPAN><SPAN class="string token">'token'</SPAN><SPAN class="punctuation token">:</SPAN>mytoken<SPAN class="punctuation token">}</SPAN>
<SPAN class="comment token"># Use POST to batch geocode</SPAN>
r <SPAN class="operator token">=</SPAN> requests<SPAN class="punctuation token">.</SPAN>post<SPAN class="punctuation token">(</SPAN>url<SPAN class="operator token">=</SPAN>URL<SPAN class="punctuation token">,</SPAN> data<SPAN class="operator token">=</SPAN>addresses<SPAN class="punctuation token">,</SPAN> params<SPAN class="operator token">=</SPAN>params<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>r<SPAN class="punctuation token">.</SPAN>json<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>r<SPAN class="punctuation token">.</SPAN>text<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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 )