Group Abstract Group Abstract

Message Boards Message Boards

1
|
18.5K Views
|
11 Replies
|
10 Total Likes
View groups...
Share
Share this post:

Mailgun API - Problem with encoding in HTTPRequest

Posted 5 years ago

I'm working with a Mailgun API update for Mathematica, based this post. After config the API account, emails works so far so good, but I discovered that when I send an attached file, the diacritics get broked.

Here is the code:

HTTPRequest["https://api.mailgun.net/v3/mydomain.com/messages",
      <|
      Method->"POST"
      ,"User"->"api:MY_API_KY"
      ,"Body" -> {
          "from" -> "test@mydomain.com"
         ,"to" -> "me@me.com"
         ,"subject" -> "With Attachment"
         ,"text" -> "Diacritics test: àáâãäåçèéêëìíî"
         , "attachment" -> File["m.JPG"]
         }
        |>
     ]//URLRead[#, "Body"]&

Here are the result, commenting and uncommenting the line: , "attachment" -> File["m.JPG"]

Diacritics Problem

I tried to add the header below, without success too:

,"Headers"-> {"Content-type" -> "multipart/form-data; charset=utf-8"}

Any help is welcome

UPDATE 01

Using the old function URLFetch, I have problem with and without sending a file.

URLFetch["https://api.mailgun.net/v3/mydomain.com/messages",
      "Method"->"POST"
      ,"MultipartElements" -> {
         {"from","text/plain"} -> "test@mydomain.com"
         ,{"to","text/plain"} -> "me@me.com"
         ,{"subject","text/plain"} -> "Hello YOUR-NAME"
         ,{"text","text/plain"} -> "Diacritics test: àáâãäåçèéêëìíî"
          }
      ,"Username"->"api"
      ,"Password"->"MY_API_KY"
  ]

UPDATE 02

I created a better toy code below using https://httpbin.org, so anyone can execute the test.

POSTED BY: Rodrigo Murta
11 Replies

Hi Rodrigo,

Thank you for starting this discussion.

You might be interested to know that we are working on a framework to create connections from Wolfram Language to external APIs in an easy way and share them as resources.

Please let me know if you want to have early access to test it. Thanks!

Posted 5 years ago

@Christian:

I volunteer to help test that new API. I'm spending too much time trying to hack something together instead of working on the problem I'm trying to solve.

THANKS

POSTED BY: Mike Besso

@Mike,

Thanks Mike. Do you mind leaving your info here in this form?

https://www.wolframcloud.com/obj/christianp/tmp/SCFrameworkBetaTestingList

Thanks

Cool Jesse! Using ExportString worked! Both for HTTPRequest and the old URLFetch form!

ExportString["Diacritics test: àáâãäåçèéêëìíî", "Text", CharacterEncoding -> "UTF-8"]

Thanks for your investigation and time!

POSTED BY: Rodrigo Murta

Here is a better toy code using httpbin.org (cool service!).

HTTPRequest["https://httpbin.org/anything",
      <|
      Method->"POST"
      ,"Body" -> {
          "text" -> "Diacritics test: àáâãäåçèéêëìíî"
          , "attachment" -> File[Export["image.png", Graphics@Circle[], ImageSize-> 1]]
         }
        |>
     ]//URLRead[#, "BodyBytes"]&//FromCharacterCode

As you can see, "text" is broke:

text: "Diacritics test: \ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd"

If you comment the attachment part, we get it right:

text: "Diacritics test: \u00e0\u00e1\u00e2\u00e3\u00e4\u00e5\u00e7\u00e8\u00e9\u00ea\u00eb\u00ec\u00ed\u00ee"
POSTED BY: Rodrigo Murta
POSTED BY: Jesse Friedman
POSTED BY: Jesse Friedman

I really appreciate your attention Jesse. Unfortunately, if I specify the type as you describe, I get de msg below:

"{
  \"message\": \"'text' parameter is not a string\"
}"

What is very strange if MIMEType is: "text/plain;charset=utf-8"

Below I did a better testable toy code.

POSTED BY: Rodrigo Murta

Hi Arnoud, thanks for your suggestion. But in this case the problem is not to Read the request, the problem is that it sends the text data wrong when URLRead is executed. It's like when I pass the File, some encode changes.

POSTED BY: Rodrigo Murta

Ok, I can't think of a solution (yet).

POSTED BY: Arnoud Buzing
POSTED BY: Arnoud Buzing
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard