Search code examples
javascriptopenweathermap

Error: Invalid URI while requesting API in nodejs


Hi I'am new in web programming and I want to practice some API usage by javascript. I tried to request basic openwatcher API call:

API call:

api.openweathermap.org/data/2.5/weather?q={city name}

example:

var request = require('request');
request("api.openweathermap.org/data/2.5/weather?q=London",function(error,response,body){
    if(!error && response.statusCode == 200){
        console.log(body)
    }
    else{
        console.log(error);
        console.log(response.statusCode)
    }
})

But it outputs an error

Error: Invalid URI "api.openweathermap.org/data/2.5/weather?q=London"

What could be the problem here?


Solution

  • It looks like request requires that you specify the protocol in the URL.
    If you prefix your URL with http:// (or, ideally, https:// if supported by the service), it should start working.