Home > database >  Using curl for pushing a file to GitHub repository
Using curl for pushing a file to GitHub repository

Time:09-17

I want to create (push) new file on the GitHub repository without git tools, (because the git tool doesn't available on my PHP host). So I had a little research and I found GitHub REST API.

I try to use curl with personal access token to upload or modify a file on my repo. here is a shell code that I found and made my changes to curl like below:

curl -i https://api.github.com/repos/username/repo/newFile.txt -X PUT 
\ -H 'Authorization: token xxxxxxxxxxxxxxxxxxxx' -d "{\"path\": \"newFile.txt\", 
\ \"message\": \"update\", \"content\": \"$(openssl base64 -A -in newFile.txt)\", \"branch\": 
\"master\",
\ \"sha\": $(curl -X GET https://api.github.com/repos/username/repo/newFile.txt  | jq .sha)}" 

but I get this error:

HTTP/1.1 404 Not Found

Server: GitHub.com

How I can fix this?

P.S. even if there is an push file by email service on GitHub or (GitLab), this can help to create file on my repo via e-mail.

CodePudding user response:

I put my working curl here, if someone need this.

For easy push, it's better make your file in base64 format (base64 {filename} or online tools) and then:

curl -X PUT -H "Authorization: token xxxxxxxxxxxxxxxxxxxx" https://api.github.com/repos/{owner}/{repo}/contents/{path}/{fileName} -d "{\"message\":\"your commit message\",\"content\":\"your file in BASE64\"}"

CodePudding user response:

It should be the "Create or update file contents" endpoint API:

PUT /repos/{owner}/{repo}/contents/{path}

(I don't see contents in your https://api.github.com/repos/username/repo/newFile.txt URL)

Make sure to use a recent PAT (Personal Access Token), as seen here.
And an example:

#! /bin/bash
 
cartella= " /var/myfolder "
 
# update the file
curl -i -X ​​PUT -H ' Authorization: token 4d013330xxxxxxxxxxxxxxx ' -d " { \" path \" : \" mattei.csv \" , \
\" message \" : \" update \" , \" content \" : \" $( openssl base64 -A -in $cartella /mattei.csv ) \" , \" branch \" : \" master \" , \
\" sha \" : $( curl -X GET https://api.github.com/repos/username/repo/contents/mattei.csv | jq .sha ) } " \
https://api.github.com/repos/username/repo/contents/mattei.csv
  • Related