How do I post JSON in the body of a REST API request using HttpClient? [C#, .NET]

08-08-2016 10:51 AM
New Contributor II

I'm attempting to call the geocodeAddresses method in the ArcGIS REST API​ in my C# application.   The "addresses" input parameter is a potentially very long JSON string containing an array of a complex type. The code below works ok and I get a response from the API if the addresses param is a short JSON string containing only a few addresses, but if it is a very long string with many addresses I get an "Invalid URI: The Uri string is too long." exception when PostAsync() is called. So what I'm doing is just calling the API with a long url.  How can I ensure the parameters are posted to the body of the request and not get an extremely long url?

string url = " ";
var values = new Dictionary<string, string>
  {"token", token},
  {"forStorage", "true"},
  {"MaxBatchSize", "1000"},
  {"outFields", "*"},
  {"f", "json"},
  {"addresses", inputJson} //inputJson is an array of a complex type

using (var client = new HttpClient())
  var content = new FormUrlEncodedContent(values);
  var response = await client.PostAsync(url, content);
  Task<string> responseString = response.Content.ReadAsStringAsync();
  string outputJson = await responseString;
  res = JsonConvert.DeserializeObject<DoStuffResult>(outputJson);
  catch (Exception ex)
  Debug.WriteLine(ex.ToString()); //"Invalid URI: The Uri string is too long."

0 Kudos
0 Replies