NodeJS HttpGet 到带有 JSON 响应的 URL [英] NodeJS HttpGet to a URL with JSON response
问题描述
我是 Node 开发的新手,我正在尝试使用带有 JSON 响应的 RESTful 协议进行服务器端 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.
我试图从轨道总线中提取的 API 并在 JSON 输出中返回数据.我对如何使用实际 URL 中的所有参数和选项发出 HTTP GET 请求感到困惑.API 及其响应甚至可以通过浏览器或使用curl"命令访问.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
如何编写 Node 服务器端代码以向资源发出 GET 请求并在 URL 中包含选项并解释 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?
提前致谢!
推荐答案
request
现已弃用.建议您使用替代方法:
- 原生 HTTP/S,
const https = require('https');
- node-fetch
- axios
- 得到
- superagent
request
is now deprecated. It is recommended you use an alternative:
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
}
})
您可以在 npm 上找到有关请求的文档:https://npmjs.org/package/request
You can find documentation for request on npm: https://npmjs.org/package/request
这篇关于NodeJS HttpGet 到带有 JSON 响应的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!