简体   繁体   中英

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. However, I know I will want to use the POST method as the documentation describes since my batches may be large.

When I try to submit the data via POST, I get a very unhelpful error message.

{'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

POST Data (raw)

{  
    "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.

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.

It turns out that ESRI wants the data to be sent as x-www-form-urlencoded, as opposed to just a JSON object. So to correctly use the endpoint, send it as formdata with the key being "addresses" and the value being the JSON records object.

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. So to correctly use the endpoint, send it as formdata with the key being "addresses" and the value being the JSON records object.

If you are looking for a Java implementation you may consider using Form object (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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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