Search code examples
c#jsonrestrestsharpesri

Formatting RestSharp request for ESRI geocoder


I have some code that formats a JSON request using RestSharp to access ESRI geocoding API. The code works, but I'm wondering if there is a better way to get the request into the correct format here is a sample of what I have and below a sample of what the request should look like.

request = new RestRequest("geocodeAddresses", Method.POST);
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");

        //Format the request properly
        var attributes = new Dictionary<string, object>();
        attributes.Add("OBJECTID", address.Address);
        attributes.Add("Address", address.Address);
        attributes.Add("City", address.City);
        attributes.Add("Region", address.State);
        attributes.Add("Postal", address.ZipCode);

        JsonObject attributesObj = new JsonObject();
        foreach (var parms in attributes)
        {
            attributesObj.Add(parms);
        }


        JsonObject recordsObj = new JsonObject();
        recordsObj.Add("attributes", attributesObj);
        JsonArray EsriRequest = new JsonArray();
        EsriRequest.Add(recordsObj);
        JsonObject addressObj = new JsonObject();
        addressObj.Add("records", EsriRequest);




        request.AddParameter("addresses",
            addressObj.ToString());
        request.AddParameter("token", esriToken.ToString());
        request.AddParameter("f", "json");
        request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
        IRestResponse<EsriAddress> responseData = client.Execute<EsriAddress>(request);

Request output sample:

http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses?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"}}]}&sourceCountry=USA&token=<YOUR TOKEN>&f=pjson

I currently only ever send one address, but in theory the api can take more then one at a time.


Solution

  • Here's how I send a batch of addresses to the geocodeAddresses method in the ArcGIS REST API. I'm not using RestSharp, just HttpClient:

    string token = GetToken();
    string url = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses"; 
    
    using (var client = new HttpClient())
    {
        using (var content = new MultipartFormDataContent())
        {
            var values = new[]
            {
                new KeyValuePair<string, string>("token", token),
                new KeyValuePair<string, string>("forStorage", "true"),
                new KeyValuePair<string, string>("MaxBatchSize", "1000"),
                new KeyValuePair<string, string>("outFields", "*"),
                new KeyValuePair<string, string>("f", "json"),
                new KeyValuePair<string, string>("addresses", inputJson)  // json string containing an array of a complex type
            };
    
            foreach (var keyValuePair in values)        
                content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
    
            var response = await client.PostAsync(url, content);
            Task<string> responseString = response.Content.ReadAsStringAsync();
            string outputJson = await responseString; 
        }
    }