1

I try to use cURL to upload a local file (e.g. docx, jpeg...) to GitHub repository? How can I specify the local file location and upload it to GitHub?

curl -X "PUT" \
     -H "Accept: application/vnd.github+json" \
     -H "Authorization: token <token>" \
               https://api.github.com/repos/BT23/demo-repo/contents/hello.txt \
               -d '{
               "message":"Upload this file to Git",
               "committer":{"name":"Bon", "email":"bon@bon.com"},
               "content":{"$(openssl base64 -A in $/temp/hello.txt)"}
               }'

Thanks

torek
  • 448,244
  • 59
  • 642
  • 775
BAST23
  • 75
  • 5
  • 2
    Does this answer your question? [Using curl for pushing a file to GitHub repository](https://stackoverflow.com/questions/69069145/using-curl-for-pushing-a-file-to-github-repository) – Mime Jul 13 '22 at 06:07
  • 1
    Besides being a potential duplicate, what is the problem with using the command `openssl base64 -in `, as it's already exist in your code snippet? – Mime Jul 13 '22 at 06:09
  • Error out "Problems parsing JSON" when running the script. – BAST23 Jul 13 '22 at 07:08
  • 1
    The value of `content` should not be a object, but instead a string, as stated in the [Github REST Documentation](https://docs.github.com/en/rest/repos/contents#create-or-update-file-contents). Simply remove the curly brackets around the value of `content`. – Mime Jul 13 '22 at 07:15
  • After removing the curly brackets, i get another error "content is not valid Base64." – BAST23 Jul 13 '22 at 22:32
  • Have you tried with `openssl base64 -A -in ` or `openssl base64 -in `? `openssl base64 -A in ` will generate the output `base64: Use -help for summary`, as a dash is missing before in to specifiy it as a option for the input file – Mime Jul 14 '22 at 05:44
  • I ran the openssl base64 -in ../temp/hello.txt -out ../temp/hello64.txt command on Git Bash alone. The command can convert the hello.txt file to base 64. When I tried to upload the converted hello64.txt file to GitHub using curl. It is throwing 'content is not valid Base64' – BAST23 Jul 14 '22 at 06:27
  • curl -X "PUT" \ -H "Accept: application/vnd.github+json" \ -H "Authorization: token " \ https://api.github.com/repos/BT23/demo-repo/contents/hello.txt \ -d '{ "message":"Upload this file to Git", "committer":{"name":"Bon", "email":"bon@bon.com"}, "content": "../temp/hello64.txt)" }' – BAST23 Jul 14 '22 at 06:32
  • `"content": "$(cat ../temp/hello64.txt)"` instead of `"content": "../temp/hello64.txt)"`, as the value of `content` MUST be the `base64`-string and not the path to the file. Also please see the submitted answer by VonC. – Mime Jul 14 '22 at 06:45

1 Answers1

1

As an alternative, you can separate the content encoding from the curl call.
See this gist

content=$(cat /temp/hello.txt | base64)
curl -X "PUT" \
     -H "Accept: application/vnd.github+json" \
     -H "Authorization: token <token>" \
               https://api.github.com/repos/BT23/demo-repo/contents/hello.txt \
               -d '{
               "message":"Upload this file to Git",
               "committer":{"name":"Bon", "email":"bon@bon.com"},
               "content":"${content}"}
               }'
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • No, it doesn't work either. Thank you for your help. I wrote C# code to convert the file to Base64 and upload it to GitHub successfully. – BAST23 Jul 15 '22 at 02:50