0

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;
    }
}
Giacomo Pirinoli
  • 548
  • 6
  • 17
  • When you use two different clients to send the *same* request and you got different results, then either both requests are not equal or the server has some changed inner state that leads to the different behavior. Maybe you can pipe your both requests (C# and PostMan) through e.g. Fiddler to better compare both. – Oliver Mar 16 '21 at 14:30
  • Even with the same request using C# code ,If I debug and run it thru twice ,thrice the same request yields results and sometimes no content – lalita sharma Mar 16 '21 at 14:33
  • 2
    Then it is a problem of the server you're calling. Ask the person or documentation of the server api you're calling. – Oliver Mar 16 '21 at 14:36
  • 1
    Due to the fact 204 means `No Content` it simply tries to tells you, that you already asked this question and the answer didn't change since the last request. – Oliver Mar 16 '21 at 14:38
  • 2
    Please do not wrap `HttpClient` into an `using` block: [YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE](https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) – Peter Csala Mar 16 '21 at 15:19
  • HttpClient is thread safe. Create it once and re-use it for all your http requests. – jjxtra Mar 16 '21 at 15:37
  • I removed the using block.But problem still persisits I am getting 204 from code whereas Postman shows data with status 200 .@Oliver ,I am running the API call in a loop and with different location Parameters so the Question asked is not same ! – lalita sharma Mar 16 '21 at 16:10
  • Are you running Postman in a tight loop as well? Could be some sort of rate limiting on the server, though 204 seems like an odd status code to use for rate limiting. If sending the exact same request in C# results in a different response, as your first comment seems to suggest, it really sounds like you need to consult the api documentation or maintainers and see if you can find out what causes a 204 to be returned. – Joshua Robinson Mar 16 '21 at 18:34

0 Answers0