'How to upload an image to use in issue comments via Github API

I'm currently working with Github API to make an iOS github client app. I'd like to implement the feature of creating issues with images. My question is how to upload an image for issue comments via API. We're able to upload the image by drag-and-drop via browser in github.com like image below: image_upload_in_browser

I'd like to use this https://cloud.githubusercontent.com/assets/~~~.PNG. Any way to upload to https://cloud.githubusercontent.com via API or something?



Solution 1:[1]

By the Feb 2020 there are still no official solution to upload files via API to be used in the github issues.

Simple solution is to use a repository branch (you can name it assets). You can link to them from the github issues easily, just use raw link with last commit SHA:

https://github.com/ORG/REPO/raw/LAST_SHA/PATH

This kind of link will be rendered in the issues body always correctly.

Solution 2:[2]

Ever I also try to find one way to make it.But there is not any available method to do it. I'm doing some extra test to find that you can observe the drag-and-drop action in your devtool-network panel.

I find that https://github.com/upload/assets/21842410 be requested with PUT method, and its response is {"id":21842410,"name":"-2.png","size":1261,"content_type":"image/png","href":"https://cloud.githubusercontent.com/assets/3518853/21842410/7c3f6812-d79b-11e6-8209-e49b44aaa883.png","original_name":null}

I've not finished my test, if this inspires you and you have time to implement any demo, please tell me your result. :)

Solution 3:[3]

A solution I'm using is to push the images to the repository instead, and use a relative link in the issue.

You can create an orphan branch just for images and push it to a ref outside of refs/heads, this way this ref is not cloned in a normal clone (you would need git clone --mirror).

It would look something like this (proof-of-concept):

git checkout --orphan images
git rm -rf *
# copy your images to the repo
git add <your images>
git commit -m "add images"
git push origin HEAD:refs/images/image-ref
git log --format=%H
# note the hash

and then in the issue, use a relative link using the commit hash from above:

![alt text](../blob/<HASH HERE>/path/to/your/image.png?raw=true)

Solution 4:[4]

It can't be done. Your only chance is to upload your image to your own s3 bucket or similar and link that in the comment/issue. I think github will pick up those and cache them for better user experienceon github.com

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 Dmitry Polushkin
Solution 2 yupengzhang
Solution 3 philb
Solution 4 Thomas R. Koll