Skip to content

Instantly share code, notes, and snippets.

@aborruso
Last active May 3, 2024 09:44
Show Gist options
  • Save aborruso/40aee4f1738c0a0e3727bf7cbec91152 to your computer and use it in GitHub Desktop.
Save aborruso/40aee4f1738c0a0e3727bf7cbec91152 to your computer and use it in GitHub Desktop.
How to update a file in github via cURL
#!/bin/bash
cartella="/var/myfolder"
# update the file
curl -i -X PUT -H 'Authorization: token 4d013330xxxxxxxxxxxxxx' -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
@defrindr
Copy link

thanks sir

@jamlevi
Copy link

jamlevi commented Feb 12, 2021

Adding that I stumbled across this, now about a year later while trying to figure out how to create a small json file via api, and after reading many many blogs & stackoverflow posts, this gist provided me the solution: the escaping of the quotes inside the data block. Thank you @aborruso! Here is my working example for those trying to base64 encode a small json snippet & curl PUT at the same time. It could be combined into a one-liner. Of course, your token has to have perms.

content=$(echo '{
   "repo":"my_repo",
   "is_production":"true",
   "maintainer":{
      "slack_channel":"my_slack_channel",
      "team_email":"my_team_email@xyz.com"
   }
}' | base64)
curl -v -i -X PUT -H "Accept: application/vnd.github.v3+json" -H "Authorization: token qqqqqqqqqqqqqq" -d "{\"message\": \"TEST-9876 add metadatafile\", \"content\": \"$content\"}" https://github.xyz.com/api/v3/repos/my_org/my_repo/contents/.metadata.json

@elect86
Copy link

elect86 commented May 7, 2021

Quite valuable indeed, grazie Andrea!

@zpostfacto
Copy link

This helped me today, thank you.

@aborruso
Copy link
Author

Only to say that I'm very happy to have helped you @defrindr @jamlevi @elect86 and @zpostfacto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment