Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github REST API full example

Tags:

rest

github

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

like image 447
Nick Avatar asked Dec 18 '17 21:12

Nick


People also ask

Does GitHub have a REST API?

GitHub provides two APIs: a REST API and a GraphQL API. You can interact with both APIs using GitHub CLI, curl, the official Octokit libraries, and third party libraries.

What is REST API example?

A REST API is a way for two computer systems to communicate using the HTTP technologies found in web browsers and servers. Sharing data between two or more systems has always been a fundamental requirement of software development. For example, consider buying motor insurance.


1 Answers

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.

like image 120
Wsl_F Avatar answered Oct 18 '22 21:10

Wsl_F