restgithub

Github REST API full example


I would like to create a new file in the my github repository by using github REST API. I've found following link, but honestly I don't understand it((

As I understand, I could do POST

url: https://api.github.com/repos/MyUserName/MyRepositoryName

headers:

Accept: application/vnd.github.v3+json

body:

{
  "message": "my commit message",
  "committer": {
    "name": "My name",
    "email": "my email"
  },
  "content": "base64encoded"
}

But it doesn't work. Could you please, write

1) which url should I call

2) which headers this request should contains

3) what body should be


Solution

  • You were close:) Lets assume, that

    1) your login: YourUsername

    2) your access token: 123a321

    3) repository to be updated: YourRepo

    4) file to be created: file.txt

    5) folder that will contains new file: f1/f2

    According to those assumptions your request should be following:

    type : PUT

    url : https://api.github.com/repos/YourUsername/YourRepo/contents/f1/f2/file.txt

    headers :

    {
     "Content-Type" : "application/vnd.github.v3+json",
     "Authorization" : "token 123a321"
    }
    

    body :

    {
      "message": "my commit message",
      "committer": {
        "name": "My name",
        "email": "my email"
      },
      "content": "base64encoded"
    }
    


    UPD If you write in java, you can use GitHubFileAPI library, that I recently pushed to the maven central repository.