node.js - 节点js将查询字符串传递给url
问题描述
我正在使用请求承诺插件请求来创建获取和发布 api。我想通过参数传递 URL。下面是我的代码。如何在 URL 中传递参数值?
async function getDetails (req) {
var options = {
url: 'http://localhost:3000/api/student/id/{studen_id}/branch/{studentbranch}',
method: 'GET',
headers: {
'User-Agent': 'my request',
'Authorization': 'Bearer {my token}',
'Content-Type': 'application/json',
'Accept': 'application/json'
},
json: true
};
let res= await rp(options)
.then(function (body) {
return body;
})
.catch(function (err) {
console.log("error get balance",err)
});
return res;
}
解决方案
async function getDetails (req) {
var options = {
url: 'http://localhost:3000/api/student/id/'+encodeURIComponent(req.body.id)+'/branch/'+encodeURIComponent(req.body.branch),
method: 'GET',
headers: {
'User-Agent': 'my request',
'Authorization': 'Bearer {my token}',
'Content-Type': 'application/json',
'Accept': 'application/json'
},
json: true
};
let res= await rp(options)
.then(function (body) {
return body;
})
.catch(function (err) {
console.log("error get balance",err)
});
return res;
}
推荐阅读
- python - Python Pandas 将 1 列字符串组合转换为多列分类数据
- python - 标记点,在单位圆内生成
- swift - 使用 SwiftUI 在 iOS 上创建应用内超链接
- javascript - 如何在 Google 表格中编写脚本以删除列中的某些值?
- swift - UITableView - 意外发现 nil Swift
- python - 无论列顺序如何,计算唯一组合
- blockchain - 为什么ERC721的铸币功能有权限控制?
- javascript - 在 Javascript 中使用关联数组重新组织 JSON 数据
- javascript - 赛普拉斯:使用或与期望
- reactjs - 如何在反应头盔中预加载最大内容绘制图像