1 python rest post geocoding arcgis-server
我正在尝试访问地理编码服务器的 REST API:
[ https://locator.stanford.edu/arcgis/rest/services/geocode/USA_StreetAddress/GeocodeServer] (ArcGIS Server 10.6.1)
...使用 POST 方法(顺便说一句,可以使用一两个示例,似乎只有关于何时使用 POST 的非常简短的“注释”,而不是如何使用:https: //developers.arcgis.com/rest /geocode/api-reference/geocoding-geocode-addresses.htm#ESRI_SECTION1_351DE4FD98FE44958C8194EC5A7BEF7D)。
我正在尝试使用 requests.post(),并且我认为我已经成功地接受了令牌等...,但我不断收到 400 错误。
根据以前的经验,这意味着数据格式有些问题,但我直接从 Esri 支持网站剪切并粘贴了这个测试对。
# 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)
Run Code Online (Sandbox Code Playgroud)
这是我一直得到的:
{'error': {'code': 400, 'message': 'Unable to complete operation.', 'details': []}}
Run Code Online (Sandbox Code Playgroud)
小智 5
我不得不花比我愿意承认的时间更长的时间来解决这个问题,但技巧(我猜)是使用正确的请求标头并使用json.dumps().
import requests
import json
url = 'http://sampleserver6.arcgisonline.com/arcgis/rest/services/Locators/SanDiego/GeocodeServer/geocodeAddresses'
headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
addresses = json.dumps({ 'records': [{ 'attributes': { 'OBJECTID': 1, 'SingleLine': '2920 Zoo Dr' }}] })
r = requests.post(url, headers = headers, data = { 'addresses': addresses, 'f':'json'})
print(r.text)
Run Code Online (Sandbox Code Playgroud)