#node.js #request #npm-request
Вопрос:
Я использую запрос плагина request promise для создания api get и post. Я хочу передать 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;
}
Ответ №1:
Чтобы передать параметр по URL-адресу, вы можете передать его следующим образом:
http://localhost:3000/api/student/?id={studen_id}amp;branch={studentbranch}
Ответ №2:
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;
}