Created
November 22, 2017 21:58
-
-
Save scripting/98a6ab727ccacac41029b50e07b3c4e2 to your computer and use it in GitHub Desktop.
A simple JavaScript demo app, running in Node, creates a new file in a repo on GitHub using the REST API
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
const request = require ("request"); | |
const fs = require ("fs"); | |
var myPost = { | |
username: "scripting", | |
repo: "test1", | |
path: "ideas/plot.html", | |
content: "<!DOCTYPE html>\n<html><body>A programmer writes a demo app. Everyone lives happily ever after.</body></html>", | |
type: "text/html", | |
committer: { | |
name: "Dave Winer", | |
email: "[email protected]" | |
}, | |
message: "Yet another testing change", | |
userAgent: "Dave's GitHub Upload Demo App" | |
}; | |
fs.readFile ("config.json", function (err, data) { | |
var config = JSON.parse (data); | |
var bodyStruct = { | |
message: myPost.message, | |
committer: { | |
name: myPost.committer.name, | |
email: myPost.committer.email | |
}, | |
content: new Buffer (myPost.content).toString ('base64') | |
}; | |
var username = myPost.username; | |
var url = "https://" + username + ":" + config.password + "@api.github.com/repos/" + username + "/" + myPost.repo + "/contents/" + myPost.path; | |
const theRequest = { | |
method: "PUT", | |
url: url, | |
body: JSON.stringify (bodyStruct), | |
headers: { | |
"User-Agent": myPost.userAgent, | |
"Content-Type": myPost.type | |
} | |
}; | |
request (theRequest, function (err, response, body) { | |
if (err) { | |
console.log (err.message); | |
} | |
else { | |
console.log (JSON.stringify (response, undefined, 4)); | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment