我面对以下问题,将参数从Express API传递到请求模块URL。
在以下代码中,假设我有请求详细信息为
request_data.url = http://localhost:3000/interface/en/
用户以http://localhost:3000/interface/en/123456
我想将123456发送到该行
url: request_data.url + acct,
因此,我的最终网址为http://localhost:3000/interface/en/123456
但是我以下代码不起作用,有人可以在这里帮助我或建议我需要哪些更改
代码
app.get('/interface/:env/:acct', (req, res) => {
var acct = req.params.acct;
var env = req.params.env;
var hsResponse = request({
proxy: proxyUrl,
url: request_data.url + acct,
headers: request_data.headers,
method: request_data.method,
form: oauth.authorize(request_data)
}, function (error, response, body) {
res.setHeader('Content-Type', 'application/json');
res.send(body); //<-- send hsResponse response body back to your API consumer
});
});
请使用以下代码,
app.get('/interface/:env/:acct', (req, res) => {
var acct = req.params.acct;
var env = req.params.env;
// here you need to update your url
request_data.url = request_data.url + acct;
var hsResponse = request({
proxy: proxyUrl,
url: request_data.url ,
headers: request_data.headers,
method: request_data.method,
form: oauth.authorize(request_data)
}, function (error, response, body) {
res.setHeader('Content-Type', 'application/json');
res.send(body); //<-- send hsResponse response body back to your API consumer
});
});
我认为您正在使用oauth,在此处将表单字段传递给请求,需要使用现有的映射request_data
(例如URL
和其他attributes
(授权。
希望这对您有帮助!