简体   繁体   English

如何使用ArcGIS REST正确格式化geocodeAddresses POST

[英]How to Properly Format geocodeAddresses POST with ArcGIS REST

I am able to successfully process a batch geocoding request (described here geocodeAddresses—ArcGIS REST API: World Geocoding Service | ArcGIS for Developers ) using a GET request. 我能够使用GET请求成功处理批处理地理编码请求(此处描述geocodeAddresses-ArcGIS REST API:世界地理编码服务| ArcGIS for Developers)。 However, I know I will want to use the POST method as the documentation describes since my batches may be large. 但是,我知道我将要使用POST方法,如文档所述,因为我的批次可能很大。

When I try to submit the data via POST, I get a very unhelpful error message. 当我尝试通过POST提交数据时,收到一条非常无用的错误消息。

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

The request I am trying to make looks like this (I have tried various iterations): 我要发出的请求看起来像这样(我尝试过各种迭代):

URL: http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses?sourceCountry=USA&token= &f=pjson 网址: http ://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses?sourceCountry=USA&token=&f = pjson

POST Data (raw) POST数据(原始)

{  
    "addresses": {  
        "records": [  
            {  
                "attributes": {  
                    "OBJECTID": 1,  
                    "Address": "380 New York St.",  
                    "City": "Redlands",  
                    "Region": "CA",  
                    "Postal": "92373"  
                }  
            },  
            {  
                "attributes": {  
                    "OBJECTID": 2,  
                    "Address": "1 World Way",  
                    "City": "Los Angeles",  
                    "Region": "CA",  
                    "Postal": "90045"  
                }  
            }  
        ]  
    }  
}  

Where of course TOKEN is replaced with a valid token I have successfully tested via a GET request. 我当然已经通过GET请求成功测试了将令牌替换为有效令牌的情况。

Variations I have tried included having "records" as the top level key and including the GET parameters such as the token as keys in the POST payload. 我尝试过的变体包括以“记录”作为顶级键,并包括GET参数(例如令牌)作为POST负载中的键。

It turns out that ESRI wants the data to be sent as x-www-form-urlencoded, as opposed to just a JSON object. 事实证明,ESRI希望数据以x-www-form-urlencoded的形式发送,而不只是JSON对象。 So to correctly use the endpoint, send it as formdata with the key being "addresses" and the value being the JSON records object. 因此,要正确使用端点,请将其作为formdata发送,其键为“ addresses”,值为JSON records对象。

I've had the same problem and as you have already pointed out: 我遇到了同样的问题,正如您已经指出的那样:

ESRI wants the data to be sent as x-www-form-urlencoded, as opposed to just a JSON object. ESRI希望数据以x-www-form-urlencoded的形式发送,而不只是JSON对象。 So to correctly use the endpoint, send it as formdata with the key being "addresses" and the value being the JSON records object. 因此,要正确使用端点,请将其作为formdata发送,其键为“ addresses”,值为JSON records对象。

If you are looking for a Java implementation you may consider using Form object (javax.ws.rs.core.Form). 如果您正在寻找Java实现,则可以考虑使用Form对象(javax.ws.rs.core.Form)。

I've done it this way: 我这样做是这样的:

// Build addresses form object
Form addressesParam = new Form();
addressesParam.param("addresses", buildAddressesParam(addresses).toString());

// Try make request and parse it into JSON node
JsonNode jsonResponse;
try {
    String response = webTarget.request(MediaType.APPLICATION_JSON).post(Entity.entity(addressesParam, MediaType.APPLICATION_FORM_URLENCODED_TYPE), String.class);
    jsonResponse = new ObjectMapper().readTree(response);           
} catch(IOException e) {
    ...
}

Where the input addresses is defined as HashMap<String, String> addresses . 输入地址定义为HashMap<String, String> addresses

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM