URL = http://example.com,
Header = [],
Type = "application/json",
Content = "我是中文",
Body = lists:concat(["{\"type\":\"0\",\"result\":[{\"url\":\"test.cn\",\"content\":\"", unicode:characters_to_list(Content), "\"}]}"]),
lager:debug("URL:~p, Body:~p~n", [URL, Body]),
HTTPOptions = [],
Options = [],
Response = httpc:request(post, {URL, Header, Type, Body}, HTTPOptions, Options),
but http request body received by http server is not 我是中文。 how to sovle this problem?
Luck of the Encoding
You must take special care to ensure input is what you think it is because it may differ from what you expect.
This answer applies to the Erlang release that I'm running which is R16B03-1. I'll try to get all of the details in here so you can test with your own install and verify.
If you don't take specific action to change it, a string will be interpreted as follows:
In the Terminal (OS X 10.9.2)
In the terminal the string is interpreted as a list of unicode characters.
In a Module
In a module, the default encoding is
latin1
and strings containing unicode characters are interpretedbytewise
lists (of UTF8 bytes).If you use data encoded like
BytewiseContent
,unicode:characters_to_list/1
will double-encode the Chinese characters andææ¯ä
will be sent to the server where you expected我是中文
.Solution
erl
command line, ensure it is setup to use unicode.bytewise
encoding to unicode before processing (this goes for binary data acquired usinghttpc:request/N
as well).If you embed unicode characters in your module, ensure that you indicate as much by commenting within the first two lines of your module:
This will change the way the module interprets the string such that:
Once you have ensured that you are concatenating characters and not bytes, the concatenation is safe. Don't use
unicode:characters_to_list/1
to convert your string/list until the whole thing has been built up.Example Code
The following function works as expected when given a
Url
and a list of unicode characterContent
:To verify results I wrote the following HTTP server using
node.js
andexpress
. The sole purpose of this dead-simple server is to sanity check the problem and solution.Gist
Verifying
Again in Erlang, the following function will check to ensure that the HTTP response contains the echoed JSON, and ensures the exact unicode characters were returned.
The complete
example.erl
module is posted in a Gist.Once you've got the example module compiled and an echo server running you'll want to run something like this in an Erlang shell:
If you've got
jiffy
set up you can also verify the content made the round trip:You should now be able to confirm round-trip encoding of any unicode content.
Translating Between Encodings
While I explained the encodings above you will have noticed that
TerminalContent
,BytewiseContent
, andUnicodeContent
are all lists of integers. You should endeavor to code in a manner that allows you to be certain what you have in hand.The oddball encoding is
bytewise
which may turn up when working with modules that are not "unicode aware". Erlang's guidance on working with unicode mentions this near the bottom under the heading Lists of UTF-8 Bytes. To translatebytewise
lists use:My Setup
As far as I know, I don't have local settings that modify Erlang's behavior. My Erlang is R16B03-1 built and distributed by Erlang Solutions, my machine runs OS X 10.9.2.