如何将YAML文件作为base64编码的字符串发送



我正试图将一个yaml文件作为base64字符串发送,以便此代码能够工作:

const response = await octokit.request('GET /repos/{owner}/{repo}/git/blobs/{file_sha}', {
owner: 'DevEx',
repo: 'hpdev-content',
file_sha: fileSha,
headers: {
authorization: `Bearer ${githubConfig?.token}`,
},
});
const decoded = Buffer.from(response.data.content, 'base64').toString('utf8');

在上述代码中,response.data.content应具有数据。

我有这条路线:

router.get('/repos/:owner/:repo/git/blobs/:file_sha', (req, res) => {
// TODO: do we need to do anything with the path params?
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { owner, repo, file_sha } = req.params;
const contents = writeUsersReport();
const encoded = Buffer.from(contents, 'binary').toString('base64');
res.send(encoded);
});

除了客户端代码期望在以下代码中的名为content的属性中使用base64字符串之外,代码运行良好:

const decoded = Buffer.from(response.data.content, 'base64').toString('utf8');

但是字符串在response.data中。如何设置content属性?

从服务器端发送一个包含具有content属性的对象的json响应,而不是直接发送编码字符串,怎么样?

// ...
const encoded = Buffer.from(contents, 'binary').toString('base64');
res.json({content:encoded});

最新更新