【问题标题】:Error: Invalid URI while requesting API in nodejs错误:在 nodejs 中请求 API 时 URI 无效
【发布时间】:2020-04-21 00:49:19
【问题描述】:

您好,我是 Web 编程新手,我想通过 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】:

    看起来request 要求您在 URL 中指定协议。
    如果你在你的 URL 前加上 http://(或者,如果服务支持,最好是 https://),它应该开始工作了。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2021-06-29
    • 2014-08-23
    • 2021-01-07
    • 1970-01-01
    • 2014-12-11
    相关资源
    最近更新 更多