10

I want to upload files from my system to a directory in github repo using the api .Is there any api endpoint which allows me to do that.

1

3 Answers 3

9

You should use the GitHub CRUD API which was introduced in .May 2013

It includes:

Sign up to request clarification or add additional context in comments.

2 Comments

I am successfully able to upload small files, but the API fails with (502 BAD Gateway) response on bigger files( > 10mb). Is there a solution for using this for bigger files?
I suspect not. You would have to use Git LFS (git-lfs.github.com, and its API (github.com/git-lfs/git-lfs/tree/main/docs/api).
3

There's deffinitly a way to do it. This is how I did it;

curl -i -X PUT -H ‘Authorization: token 9xxxxxxxxxxxxxxxxxxxxxxxe2’ -d
‘{“message”: “uploading a sample pdf”,
“content”:”bXkgbm……………………………..”
}’ https://api.github.com/repos/batman/toys/contents/sample.pdf

Where the content property is a base64 encoded string of characters. I used this tool to encode my pdf file. https://www.freeformatter.com/base64-encoder.html

Notice, "batman" is the owner, "toys" is my repo, "contents" has to be there by default, and sample.pdf would the name of the file you want to upload your file as. In short, stick to this format: /repos/:owner/:repo/contents/:path And you can run the identical step for any of these files: PNG (.png) GIF (.gif) JPEG (.jpg) Log files (.log) Microsoft Word (.docx), Powerpoint (.pptx), and Excel (.xlsx) documents Text files (.txt) PDFs (.pdf) ZIP (.zip, .gz)

Good luck. Btw, I have these same details added on here: http://www.simplexanswer.com/2019/05/github-api-how-to-upload-a-file/

1 Comment

How do you specify a complex path (nested)
0

i thought i would write up a more complete answer to this question

as per the documentation docs.github.com : Create or update file contents


you will need an access token


at the time of writing, while logged into your account :

tl;dr github.com : Fine-grained personal access tokens

  1. click on your avatar in the top right to bring up the menu
  2. click settings ( towards the bottom )
  3. choose developer settings ( at the bottom )
  4. click personal access tokens
  5. click fine-grained tokens
  6. click generate token
  7. fill in the details
  8. you will need to add "contents" permissions
  9. copy your token

notes :

  • the content must be base64 encoded
  • all uploads require a commit message
  • for updating files you will need the sha value of the file you are updating

so to upload a file to a repository say github.com : http-file-upload

(async()=>{


      console.clear();


      var file        = `
      
            //  my test file
                  
      `;
      var message     = 'my commit message';


      var owner       = 'javascript-2020';
      var repo        = 'code-projects';
      var path        = 'http-file-upload/http-file-upload.js';
      
      var token       = '<your-github-token>';
      
      var url         = `https://api.github.com/repos/${owner}/${repo}/contents/${path}`;


      var res         = await fetch(url);
      var json        = await res.json();
      var sha         = json.sha;
      
      
      var content     = window.btoa(file);
      
      var body        = {content,sha,message};      
      body            = JSON.stringify(body);
      
      var headers     = {authorization:`Bearer ${token}`};
      var opts        = {method:'put',headers,body};
      
      var res         = await fetch(url,opts);
      var json        = await res.json();

      
      console.log('result :',res.status,res.statusText);
      console.log();
      console.log(JSON.stringify(json,null,4));
      
      
})();

et voilà!

i prefer this approach because it does not require downloading any libraries

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.