Share via

Api Call Fails with 500 despite returning Data

Anonymous
Apr 4, 2024, 5:22 PM

The below code is failing with "the server responded with a status of 500 ()"

But data is being populated in elevation, I am trying to populate the elevation in browser using ajax call

but couldn't succeed.

https://api.open-meteo.com/v1/elevation?latitude=52.52&longitude=13.41

https://open-meteo.com/en/docs/elevation-api

string urlel = $"https://api.open-meteo.com/v1/elevation?latitude={data.latitude}&longitude={data.longitude}";
            var responseel = client.GetAsync(urlel).Result;
            string jsonel = await responseel.Content.ReadAsStringAsync();
            dynamic datael = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonel);
            int elevation = datael.elevation;
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,726 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 69,036 Reputation points
    Apr 4, 2024, 7:55 PM

    your sample url returns

    {
        "elevation": [
            38.0
        ]
    }
    
    

    so elevation is an array of numbers, not an int. try:

    double elevation = datael.elevation[0];

    1 person found this answer helpful.

0 additional answers

Sort by: Oldest

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.