Skip to content

Instantly share code, notes, and snippets.

@RayyanNafees
Created January 16, 2024 15:22
Show Gist options
  • Save RayyanNafees/ec80ae97fe135ffd2d3c9cb5ec6a5f22 to your computer and use it in GitHub Desktop.
Save RayyanNafees/ec80ae97fe135ffd2d3c9cb5ec6a5f22 to your computer and use it in GitHub Desktop.
Github Upload API - Dyamically create files using Github REST API shown with JS
let token = "<Github-Auth-Token>"
let fileContent = '<file-content>';
let path = '/path/to/abc.txt'
let commitMsg = 'txt file'
var content = btoa(fileContent);
uploadFileApi(token, content)
function uploadFileApi(token, content) {
var body = JSON.stringify({
message: commitMsg,
content: btoa(content)
});
var config = {
method: 'PUT',
url: 'https://api.github.com/repos/RayyanNafees/pb-admin/contents/abc.txt',
headers: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json'
},
body
};
fetch(config.url, config)
.then(r=>r.json())
.then(console.log)
.catch(console.error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment