I have a Get API call to WebAPI using GetAsync method that fetches some values in JSON based on a location parameter. I am running the WebAPI call in a loop and for some reason, for certain Location parameters, the code returns "No content" status code 204. When I call the webAPI from Postman, it returns the data successfully.
Here is my code:
private static async Task<string[]> GetLatLongValues(string locationDescription)
{
string[] latLng = new string[2];
string locationAPI = "https://zoek-search-api-live-us-2.azurewebsites.net/" ;
try
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(locationAPI);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("cache-control", "no-cache");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//HTTP GET
var responseTask = await client.GetAsync("Geocode/SearchLocation?location=" + Uri.EscapeDataString(locationDescription)); //.ConfigureAwait(continueOnCapturedContext: false);
responseTask.EnsureSuccessStatusCode();
if (responseTask.IsSuccessStatusCode)
{
var resString = await responseTask.Content.ReadAsStringAsync ();
//resString.Wait();
if (resString.Length !=0)
{
Newtonsoft.Json.Linq.JArray resArr = Newtonsoft.Json.Linq.JArray.Parse(resString);
latLng[0] = resArr[0]["latitude"].ToString();
latLng[1] = resArr[0]["longitude"].ToString();
validLatLong.Add(locationDescription, latLng);
}
else
{
latLng[0] = "0";
latLng[1] = "0";
InvalidLatLong.Add(locationDescription, latLng);
isLocValid = false;
}
}
}
return latLng;
}
catch(Exception ex)
{
latLng[0] = "0.0";
latLng[1] = "0.0";
return latLng;
}
}