Click here to Skip to main content
15,885,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm very new to consuming APIs. I'm calling a weather API in C#.Currently I'm making weather Application in UWP ( Universal Windows Platform ). When I run the following code .
C#
var client = new HttpClient();
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,
                    "api.openweathermap.org/data/2.5/weather?q=" + cityName + "," + countryName);
                var response = await client.SendAsync(request);
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var result = response.Content;
                    string restultString = await result.ReadAsStringAsync();
                    // var data = JsonConvert.
                    var data = JsonConvert.DeserializeObject<RootObject>(restultString);
                    lblWeatherResults.Text = data.sys.country.ToString() +" - "+ data.main.temp.ToString();
                }

I receive error which is asking me to provide base url and or absolute URL. I'm unable to figure out why this error coming.
this is the error i'm getting .
An Invalid request URL was provided. The ruquest URL must either be an absolute URL or BaseAddress must be set. 


here is the API which i'm trying to configure into my application using C#.

What I have tried:

I search it on Google but could't find useful material. It is asking for the correct URL but URL Format specified here is exactly the same which i'm trying .
Posted
Updated 21-May-16 22:44pm

You need to provide the protocol in the request. Try:
C#
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get,
    "http://api.openweathermap.org/data/2.5/weather?q=" + cityName + "," + countryName);
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 22-May-16 4:46am    
Actually, you have not to define the protocol, but to turn the URI to absolute (in thhis case it is a root one) by adding a preceding '//' to it...
It is even better as the service may be available both in secured and unsecured form, and you may call it both from secured and unsecured sites - all will be smooth if you do not! define the protocol...
Ammar Shaukat 22-May-16 5:08am    
fine its working . thanks.
The problem is that 'api.openweathermap.org/data/2.5/weather' is a relative URI...Relative to you current address...
You have to state, that this is a absolute one, buy defining it's root (origin)...
In your case it should be '//api.openweathermap.org/data/2.5/weather'...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900