'How to upload github asset file using CURL

I want to upload a file on my desktop called 'hello.txt' to my git repository which has a release. How do I do this? I read the git documentation but it says something like :

POST https://<upload_url>/repos/:owner/:repo/releases/:id/assets?name. How to do this in CURL. I did not understand this.

How to post this file as a release asset to my github release? Thanks



Solution 1:[1]

curl \
    -H "Authorization: token $GITHUB_TOKEN" \
    -H "Content-Type: $(file -b --mime-type $FILE)" \
    --data-binary @$FILE \
    "https://uploads.github.com/repos/hubot/singularity/releases/123/assets?name=$(basename $FILE)"

Solution 2:[2]

Extending upon @galeksandrp's answer, here are some issues I encountered

Note that the --data-binary option copies the file contents first to RAM, so if you have large files say close to 2048MB i.e. the absolute limit for github releases and if the RAM isn't enough, it fails with curl: option -d: out of memory.

The fix for that is to use -T file path (without the @).

And also on a side note, if you want to see the upload progress, you need to pipe the output to cat such as curl <...the whole command> | cat

So the complete command would look like this

curl -X POST \
    -H "Content-Length: <file size in bytes>" \
    -H "Content-Type: $(file -b --mime-type $FILE)" \ #from @galeksandrp's answer
    -T "path/to/large/file.ext" \
    -H "Authorization: token $GITHUB_TOKEN" \
    -H "Accept: application/vnd.github.v3+json" \ 
    https://uploads.github.com/repos/<username>/<repo>/releases/<id>/assets?name=<name> | cat

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Tournoij
Solution 2