Skip to content

Instantly share code, notes, and snippets.

@fijimunkii
Created June 5, 2019 14:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fijimunkii/ed2ddb7cf24c3f30293e33ca87983476 to your computer and use it in GitHub Desktop.
Save fijimunkii/ed2ddb7cf24c3f30293e33ca87983476 to your computer and use it in GitHub Desktop.
Update a gist with node core
const data = JSON.stringify({a:1,b:2,c:3},null,2);
const GIST_ID = 'axdl090asdfojal3rwaf';
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
return new Promise((resolve, reject) => {
const postData = JSON.stringify({files:{"ua.json":{content:data}}});
const options = {
hostname: 'api.github.com',
port: 443,
path: `/gists/${GIST_ID}`,
method: 'PATCH',
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `token ${GITHUB_TOKEN}`,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
'User-Agent': 'fijimunkii'
}
};
const req = https.request(options, async (res) => {
let resBody = [];
res.on('data', d => resBody.push(d));
res.on('end', () => {
if (res.statusCode !== 200) {
reject(Buffer.from(resBody).toString('utf8'));
}
resolve();
});
});
req.on('error', reject);
req.write(postData);
req.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment