Created
January 16, 2024 15:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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