我有一个方法被调用,传递一个文件名,然后对后端进行 API 调用以获取预授权链接。我试图将参数(文件名)附加到 URL 的末尾,但它导致我的 API 请求变为 404。
我如何才能正确地通过我的 API 请求传递参数并在响应中取回预授权的 URL?
我的文件名也可能包含空格,我怀疑这可能与返回 404 的请求有关。
这是在我的前端上的调用:
getPreauthorizedLink(fileName) {
console.log(fileName);
var fileName = fileName;
var url = 'reportPresignedURL/' + fileName;
fetch(config.api.urlFor(url))
.then((response) => response.json())
.then((url) => {
console.log(url);
});
}
这是该 API 在后端的实现:
reports.get('/reportPresignedURL/:fileName', async (req, res) => {
const subscriberID = req.query.subscriberID || 0;
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
var params = {
Bucket: config.reportBucket,
Key: req.params.fileName,
Expires: 60 * 5
}
try {
s3.getSignedUrl('getObject', params, function (err, url) {
if(err)throw err;
console.log(url)
res.json(url);
});
} catch (err) {
res.status(500).send(err.toString());
}
});
我已尝试通过在前端上执行以下操作将问题归结为我如何传递参数:
getPreauthorizedLink(fileName){
console.log(fileName);
var fileName = fileName;
var testFileName = 'test';
var url = 'reportPresignedURL/' + testFileName.replace(/ /g, '%20').replace(/\//g, '%2F');
fetch(config.api.urlFor(url))
.then((response) => response.json())
.then((url) => {
console.log(url);
});
}
这是我在 config.js 中指定 API 路由的方式:
reportPresignedURL: '/reports/reportPresignedURL',
我也试过这样指定它:
reportPresignedURL: '/reports/reportPresignedURL/:fileName',
供您引用,我已将此 API 路由添加到前端的 config.js。
最佳答案
你可以使用es6语法
让 url = reportPresignedURL/${fileName}
;
对于查询参数
让 url = reportPresignedURL/?fileName=${fileName}
;
关于javascript - 将参数附加到 api 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54085392/