NodeJS HttpGet到带有JSON响应的URL [英] NodeJS HttpGet to a URL with JSON response
问题描述
我刚开始使用Node开发,我想使用RESTful协议和JSON响应来进行服务器端API调用。我已阅读了 API文档和此 SO post 。
我试图从轨道总线和返回JSON输出中的数据。我很困惑如何使用实际URL中的所有参数和选项来发出HTTP GET请求。 API和它的响应甚至可以通过浏览器或使用'curl'命令来访问。 http://developer.cumtd.com/api /v2.2/json/GetStop?key=d99803c970a04223998cabd90a741633&stop_id=it
如何编写Node服务器端代码以向GET请求发送
/ div>
请求模块使这很容易。
var request = require(request)从npm安装请求到您的包中
var url =http://developer.cumtd.com/api/v2.2/json/GetStop? +
key = d99803c970a04223998cabd90a741633+
& stop_id = it
请求({
url:url,
json:true
},function(error,response,body){
if(!error&& response.statusCode === 200){
console.log打印json响应
}
})
在npm: https://npmjs.org/package/request
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.
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
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?
Thanks in advance!
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
}
})
You can find documentation for request on npm: https://npmjs.org/package/request
这篇关于NodeJS HttpGet到带有JSON响应的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!