Search code examples
c#weather-api

National Weather Service API Internal server error


I keep getting error 500 when trying to get data using their new API, even when I include UserAgent string that they request: https://forecast-v3.weather.gov/documentation

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.weather.gov/points/40,-90/forecast/hourly");

request.UserAgent = "WeatherTest/v1.0 (http://www.website.com/; Email@email.com)";

// exception occurs here
using (Stream stream = request.GetResponse().GetResponseStream())
{
    using (StreamReader streamReader = new StreamReader(stream))
    {
        var jsonData = streamReader.ReadToEnd();
    }
}

But I can use the endpoint on a browser just fine using the same url: https://api.weather.gov/points/40,-90/forecast/hourly

I have also tested this code with another API (Weather Underground) and it works, but my boss wants me to use National Weather Service api instead. Any help is appreciated!


Solution

  • The documentation you provided gives you an answer:

    Content Negotiation

    The API will use Accept headers to modify the response returned. See the FAQ tab for more information. Parameters include: Version of the API, defaults to the oldest Format of the response, default in specifications An example of the Accept header would be "Accept: application/vnd.noaa.dwml+xml;version=1"

    Add:

    request.Headers.Add("Accept", "application/vnd.noaa.dwml+json;version=1"); and you'll get a JSON response.