# How to upload an image to the Stack Exchange Imgur account?

This is not a duplicate of How to call Imgur API.

I tried to upload to imgur using its API, but (as my friends test) the API of imgur cannot be called in Chinese mainland. Then I found that halirutan has a technology to upload an image to imgur even without any account information via http://stackoverflow.com/upload/image. Now I can upload image to imgur by his function SEUploadersendImage. Little pity is that he implemented it with Java code.

We can find his code here. I get nothing after researching his Java code. Can we do the same thing in pure Mathematica using new functions introduced in version 11.0 such as HTTPRequest, URLRead and so on?

This is expected to work with jpg and png files. You need to input the path to image, just like an upload.

uploadImageSE[imagePath_] :=
Module[{req},
req = HTTPRequest[

"Body" -> {"image" -> <|"Content" -> File[imagePath],
"MIMEType" -> "image/png"|>}
|>];
StringCases[URLRead[req, "Body"], "http" ~~ __ ~~ ".jpg" | ".png"] //
First]


• If you've already set "MIMEType" -> "image/png", then you don't need to search for ".jpg". You can do something like First[StringCases[URLRead[req, "Body"], RegularExpression["closeDialog$\"(.+)\"$"] -> "\$1"]]. – J. M. will be back soon Apr 5 '17 at 4:35
• @J.M. even if I set it to be png, I found the uploaded file to be .jpg sometimes when the original file is .jpg – happy fish Apr 5 '17 at 5:35
• Ah. In that case, the StringCases[]` code I gave should work no matter what the file extension is. – J. M. will be back soon Apr 5 '17 at 6:25