Showing results for 
Search instead for 
Did you mean: 
Ground Controller Lvl 1
Message 1 of 3

How to upload a file using GitHub API

Maybe it's just me, but this guide is incredibly vague.
Simple examples would be handy, and not so much emphasis on what the return messages.

The objective is to upload a file, preferably .pdf or .png through a curl command.
Any help/pointers will be highly appreciated.

2 Replies
Community Manager
Message 2 of 3

Re: How to upload a file using GitHub API

It's hard to write documentation that is both approachable but also has a good amount of detail to answer whatever questions that people will have.


So you're going to need to understand a few things about the information in the create a file endpoint reference:


  • You're going to need to be authenticated and have write permissions on the repository
  • You need to pass certain things in the URL and others in the body of the HTTP request
    • Replaceable parameters in the URL start with a colon `:`


I'm also going to be using HTTPie instead of curl because it makes JSON REST API interactions much easier. (And I was never very good at using curl for things 😀)


I used the following command:


http PUT \ 
  "Authorization: token REDACTED-TOKEN" \ 
  message="my commit message" \
committer:="{ \"name\": \"Lee Dohm\", \"email\": \"\" }" \ content="bXkgbmV3IGZpbGUgY29udGVudHM="

which resulted in the response:


    "commit": {
        "author": {
            "date": "2019-05-28T23:10:17Z",
            "email": "",
            "name": "Lee Dohm"
        "committer": {
            "date": "2019-05-28T23:10:17Z",
            "email": "",
            "name": "Lee Dohm"
        "html_url": "",
        "message": "my commit message",
        "node_id": "MDY6Q29tbWl0MTEyMTM3MDU2Ojc2YzM2Y2VjOTY3YzRkNjQ3NDIyY2Y0MTk4MjgyOWQxNTNmYThjY2U=",
        "parents": [
                "html_url": "",
                "sha": "d775caeeac9a48eb73f8012349c36a8c2b8c6e63",
                "url": ""
        "sha": "76c36cec967c4d647422cf41982829d153fa8cce",
        "tree": {
            "sha": "1f3c23004c5252ee11cb723e3319d26d91bc19ac",
            "url": ""
        "url": "",
        "verification": {
            "payload": null,
            "reason": "unsigned",
            "signature": null,
            "verified": false
    "content": {
        "_links": {
            "git": "",
            "html": "",
            "self": ""
        "download_url": "",
        "git_url": "",
        "html_url": "",
        "name": "hello.txt",
        "path": "hello.txt",
        "sha": "0d5a690c8fad5e605a6e8766295d9d459d65de42",
        "size": 20,
        "type": "file",
        "url": ""

and generated this commit.


I hope that helps!




Copilot Lvl 2
Message 3 of 3

Re: How to upload a file using GitHub API

Maybe it will help to Someone:

$file_git = "wall.jpg";
$data_git = array(
'content'=> base64_encode($file_git),
'committer'=> array(
'email' => ''
$data_string_git = json_encode($data_git);
$ch_git = curl_init('');
curl_setopt($ch_git, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch_git, CURLOPT_POSTFIELDS, $data_string_git);
curl_setopt($ch_git, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_git, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 YaBrowser/ Yowser/2.5 Safari/537.36',
$result_git = curl_exec($ch_git);
echo $result_git;
$p_git = json_decode($result_git);

Tested on PHP 7.
If the code is working, then I wouldn’t refuse likes, in gratitude for the little work :)