错误:在Node.js中请求API时出现无效的URI

问题描述 投票:0回答:1

[嗨,我是网络编程的新手,我想通过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=London”

这里可能是什么问题?

javascript api openweathermap
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.