HTTPGET的NodeJS与JSON响应一个网址 [英] NodeJS HttpGet to a URL with JSON response

查看:234
本文介绍了HTTPGET的NodeJS与JSON响应一个网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的节点发展,我试图让使用REST风格的协议与JSON响应服务器端API调用。我读了关于 API文档这的 SO帖子

I'm new to Node development and I'm trying to make a server-side API call using a RESTful protocol with a JSON response. I've read up on both the API documentation and this SO post.

这是我试图从轨道总线和返回数据拉一个JSON输出API。我对如何在实际的URL所有参数和选项的HTTP GET请求困惑。这个API和它的反应甚至可以通过浏览器或使用卷曲命令进行访问。 <一href=\"http://developer.cumtd.com/api/v2.2/json/GetStop?key=d99803c970a04223998cabd90a741633&stop_id=it\">http://developer.cumtd.com/api/v2.2/json/GetStop?key=d99803c970a04223998cabd90a741633&stop_id=it

The API that I'm trying to pull from tracks busses and returns data in a JSON output. I'm confused on how to make a HTTP GET request with all parameters and options in the actual URL. The API and it's response can even be accessed through a browser or using the 'curl' command. http://developer.cumtd.com/api/v2.2/json/GetStop?key=d99803c970a04223998cabd90a741633&stop_id=it

我怎样写节点服务器端code键使GET请求的资源在该URL和跨preT JSON的应对方案?

How do I write Node server-side code to make GET requests to a resource with options in the URL and interpret the JSON response?

在此先感谢!

推荐答案

请求模块使这很容易。要求安装到你的包裹从NPM,然后你可以做一个GET请求。

The request module makes this really easy. Install request into your package from npm, and then you can make a get request.

var request = require("request")

var url = "http://developer.cumtd.com/api/v2.2/json/GetStop?" +
    "key=d99803c970a04223998cabd90a741633" +
    "&stop_id=it"

request({
    url: url,
    json: true
}, function (error, response, body) {

    if (!error && response.statusCode === 200) {
        console.log(body) // Print the json response
    }
})

您可以找到关于故宫的请求文档: https://npmjs.org/package/request

You can find documentation for request on npm: https://npmjs.org/package/request

这篇关于HTTPGET的NodeJS与JSON响应一个网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆