节点 js 将查询字符串传递给 url

Node js passing query string to url

我正在使用请求承诺插件 request 来创建 get 和 post 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;
    
}

要通过URL传递参数,可以这样传递:

http://localhost:3000/api/student/?id={studen_id}&branch={studentbranch}
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;
    
}