I think the reality is actually a bit more subtle than what httpbin shows. The raw HTTP request without "attachment"
looks like this:
POST /anything HTTP/1.1
Host: httpbin.org
Accept: */*
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
User-Agent: Wolfram HTTPClient 12.2
Connection: close
Content-Length: 112
text=Diacritics%20test%3A%20%C3%A0%C3%A1%C3%A2%C3%A3%C3%A4%C3%A5%C3%A7%C3%A8%C3%A9%C3%AA%C3%AB%C3%AC%C3%AD%C3%AE
(...)
The "text"
parameter is sent as a URL-encoded query string. If other string parameters are added to the request, they're appended to this single query string. There's no binary encoding issue because the extended ASCII characters are simply percent-encoded.
With "attachment"
, it looks like this:
POST /anything HTTP/1.1
Host: httpbin.org
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: Wolfram HTTPClient 12.2
Connection: close
Content-Length: 778
Content-Type: multipart/form-data; boundary=------------------------a073b7b6522cb4a2
--------------------------a073b7b6522cb4a2
Content-Disposition: form-data; name="text"
Content-Type:
Diacritics test: ��������������
--------------------------a073b7b6522cb4a2
Content-Disposition: form-data; name="attachment"; filename="image.png"
Content-Type: image/png
(...)
Now it's a multipart form request. New string parameters are added as independent parts in the request body. Note that the special characters are sent as single ASCII bytes (0xE0, 0xE1, etc.) instead of as UTF-8-encoded chunks. It seems like a bug (although I'm not certain) that the string parameters are being given empty Content-Types, which the solution I gave addresses, but that's not helping with the underlying problem. Try this:
HTTPRequest[
"https://httpbin.org/anything", <|
Method -> "POST",
"Body" -> {"text" ->
ExportString["Diacritics test: àáâãäåçèéêëìíî", "Text",
CharacterEncoding -> "UTF-8"],
"attachment" ->
File[Export["image.png", Graphics@Circle[],
ImageSize -> 1]]}|>] //
URLRead[#, "BodyBytes"] & // FromCharacterCode
(The CharacterEncoding -> "UTF-8"
setting isn't necessary, but it's good to be explicit.)
This returns:
(...)
text: "Diacritics test: \u00e0\u00e1\u00e2\u00e3\u00e4\u00e5\u00e7\u00e8\u00e9\u00ea\u00eb\u00ec\u00ed\u00ee"
(...)
And the raw request body looks like:
POST /anything HTTP/1.1
Host: httpbin.org
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: Wolfram HTTPClient 12.2
Connection: close
Content-Length: 787
Content-Type: multipart/form-data; boundary=------------------------3b920b8202986fa1
--------------------------3b920b8202986fa1
Content-Disposition: form-data; name="text"
Content-Type:
Diacritics test: àáâãäåçèéêëìíî
--------------------------3b920b8202986fa1
Content-Disposition: form-data; name="attachment"; filename="image.png"
Content-Type: image/png
(...)
You may wish to specify an explicit Content-Type using an association as in my previous reply, although it may not matter to the API you're talking to.
If this doesn't work I can try getting my own Mailgun API key to test with.
P.S. https://webhook.site/ is useful for debugging issues like this (although I still had to look at the raw request data to see what was going on).