Skip to content
Advertisement

How do I send a YAML file as a base64 encoded string?

I am trying to send a yaml file as a base64 string so that this code works:

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');

In the above code response.data.content should have the data.

I have this route:

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);
});

The code is working fine except that the client code expects the base64 string in a property called content in the following code:

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

But the string is in response.data. How can I set the content property instead?

Advertisement

Answer

How about sending a json response containing an object with a content property from your server side instead of the encoded string directly?

// ...
const encoded = Buffer.from(contents, 'binary').toString('base64');
res.json({content:encoded});
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement