I want to call this Picture bed API to upload my picture and return its link.I have translated the official instructional the page into English.And there is a example succeed to use it with Python. I want to use it in Mathematica also, but the parameter (smfile) seem is Python image object.Such as following try.So how to make it?The img in code is


Try one:

URLExecute["https://sm.ms/api/upload", <|"Method" -> "POST", 
  "smfile" -> img|>]

Try two:

URLExecute["https://sm.ms/api/upload", <|"Method" -> "POST", 
  "smfile" -> 
   FromCharacterCode[ToCharacterCode[ImageData[img], "UTF-8"], 

I think the key of failure is that "smfile" should be a Python object but not a common Mathematica expression or maybe there are other workaround can do this.

  • $\begingroup$ Please make this question self contained. Include your mma code and explanation what is going wrong. $\endgroup$
    – Kuba
    Oct 14, 2016 at 9:17
  • $\begingroup$ "smfile is a Python image object" -- the docs suggest it a normal PNG file. Have you tried the method in the post that you commented on (mathematica.stackexchange.com/a/97658/1043)? $\endgroup$
    – ZachB
    Oct 14, 2016 at 17:25
  • $\begingroup$ @ZachB Yes,I have noted that post and give some try,but I am lack of this area knowledge.I don't know how to do it still. $\endgroup$
    – yode
    Oct 14, 2016 at 17:30

2 Answers 2


Using URLFetch[]:

smmsUpload[img_, detailed : (True | False) : False] := Module[{raw}, 
    raw = ImportString[URLFetch["https://sm.ms/api/upload", Method -> "POST", 
                                "MultipartElements" -> {{"smfile\"; filename=\"tmp.png", 
                                "image/png"} -> ExportString[img, "PNG"]}], "RawJSON"];
    If[raw === $Failed || Lookup[raw, "code", "error"] =!= "success",
       Echo[raw["msg"]]; Return[$Failed]];
    If[! detailed, raw["data", "url"],
       Grid[Transpose[{{"Dimensions:", "Hash:", "Image Link:", "Delete Link:"}, 
                       MapAt[Hyperlink, FlattenAt[TakeDrop[
                       Lookup[raw["data"], {"width", "height", "hash", "url", "delete"}],
                              2], 2], {{3}, {4}}]}], Alignment -> Left]]]

smmsUpload[ExampleData[{"AerialImage", "Pentagon"}], True]


I couldn't get URLRead[]/URLExecute[] to work, so I'll leave that for somebody else to do.

  • $\begingroup$ That If scare me, actually this answer is what I want exactly which can upload a image produced compute.Thanks very much,maybe you can change the URLFetch to URLRead enlightened by SqRoots's answer. $\endgroup$
    – yode
    Oct 15, 2016 at 8:24
  • $\begingroup$ As your answer,I think HTTPRequest["https://sm.ms/api/upload",<|Method -> "POST","Body"->{"smfile"-><|"Content" -> ExportString[img,"PNG"],"MIMEType" -> "image/png","filename" -> "tmp.png"|>}|>] should work,but I don't know what happen. $\endgroup$
    – yode
    Oct 15, 2016 at 8:50
  • $\begingroup$ Did you try your proposal already if it works? $\endgroup$ Oct 15, 2016 at 9:08
  • $\begingroup$ That work,Would you mind update your answer? $\endgroup$
    – yode
    Oct 16, 2016 at 3:05

In Mma 11, you can use this function URLRead[].


image = "E:/yourPic.jpg";(*Your Picture*)
url = "https://sm.ms/api/upload";(*Picture Bed URL*)
req = HTTPRequest[url, <|"Body" -> {"smfile" -> <|"Content" -> File[image], 
    "Name" -> FileNameTake[image]|>}|>];(*Upload Request, the "Name" is unnecessary*)
res = URLRead[req, "Body"];(*Upload, return JSON String*)
assoc = ImportString[res, "RawJSON"](*convert to Association*)

Then, assoc is what you want.

  • URL: assoc["data"]["url"]
  • delete: ImportString[URLRead[assoc["data"]["delete"], "Body"]]
  • size: assoc["data"]["size"]
  • width: assoc["data"]["width"]
  • height: assoc["data"]["height"]
  • $\begingroup$ "Name" is certainly unnecessary if the image is coming from a file, but what would you propose if the image is already in Mathematica? Exporting it as a file just to upload it seems wasteful... $\endgroup$ Oct 15, 2016 at 8:59
  • $\begingroup$ I think it is wasteful for I/O, but not to much for CPU. yode's way works, and not need export. $\endgroup$
    – Li Xuan
    Oct 16, 2016 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.