错误:在Node.js中请求API时,URI无效

时间:2020-01-02 10:36:50

标签: javascript api openweathermap

嗨,我是网络编程的新手,我想通过javascript练习一些API用法。 我尝试请求基本的openwatcher API调用:

API调用:

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

示例:

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)
    }
})

但输出错误

错误:无效的URI“ api.openweathermap.org/data/2.5/weather?q=伦敦”

这里可能是什么问题?

1 个答案:

答案 0 :(得分:0)

request似乎要求您在URL中指定协议。
如果您为URL加上http://前缀(或者,如果服务支持,最好为https://),它应该可以开始工作。