Search code examples
jsonimagegithubgithub-api

Commit image in git tree using the GitHub api


When we use the GitHub REST API to create git trees, our json data that needs to be POSTed looks like this:

{"tree": [{"path": "final.txt", "mode": "100644", "type": "blob", "content": "some content"}, {"path": "another_one.txt", "mode": "100644", "type": "blob", "content": "some more content"}]}

Is it possible to commit an image to GitHub with this format?


Solution

  • Okay, so you can encode the image in base64, then make a post request with that base64 string to create a blob using https://api.github.com/repos/user/repo/git/blobs. Get the given sha of the blob and then create a json object as so:

    "tree": [{"path": "image.png", "mode": "100644", "type": "blob", "sha": "blob"}]

    Post this json object to the GitHub tree API and your image will be uploaded to your repository.