如何在 NodeJS request 中使用查询字符串参数
问题:
你正在使用 request 库发出 HTTP GET 请求:
request_example_simple.js
const request = require("request")
request.get("http://localhost:8000", function(err, response, body) {
console.log(err, body);
})现在你尝试向请求添加查询参数。对于此示例,我们假设你要添加一个参数:foo=bar
解决方案
你可以像这样使用 qs 参数:
request_example_qs.js
const request = require("request")
request.get({url: "http://localhost:8000", qs: {"foo": "bar"}}, function(err, response, body) {
console.log(err, body);
})注意仅向 request.get 添加 qs 参数不起作用,你需要有一个字典作为第一个参数,至少包含 {"url": <你的 URL>, "qs": {<一个或多个查询参数>}}
感谢 Daniel 在 StackOverflow
Check out similar posts by category:
Javascript
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow