1

I am sending from curl to a service with REST API and I don’t understand why the server is not receiving special characters ÆØÅ correct.

When I am doing it from curl in CMD, the server is receiving the Danish characters ÆØÅ incorrect.

When I am using the browser, through StrongLoop API, the characters are correctly inserted.

As far as I have been able to research, I need the charset defined. But this have no effect. Using chcp 65001 and Lucida Console font does not help. I am a bit lost identifying the problem.

This is the command used:

curl -X POST -H "Content-Type: application/json; charset=UTF-8" -H "Accept: application/json" -d "{  \"metadata\": {    \"user\": \"aa\",    \"version\": \"ED\",    \"transaction_date\": \"2016-12-30\",    \"context_info\": \"string\"  },  \"container\": {    \"type\": \"SC\",    \"type_source\": \"string\",    \"op_status_source\": \"string\",    \"identification\": \"øæå\",  \"id_source\": \"876332d2321\",    \"ver\": 0  }}" "http://xx.com/xx/containers?access_token=xxx

The response:

{"type":"SC","type_source":"string","op_status_source":"string","identification":"���","id_source":"8763wwd32d2321","ver":1}

æøå will either be ∩┐╜∩┐╜∩┐╜", ���, or similar, depending on the chcp.

Also note, the console will display æøå correctly, but not the response.

Adam Westh
  • 27
  • 1
  • 5
  • Dunno by maybe try with `--data-binary` instead of `-d`? (Despite the “binary” in the name of the option, it’s not (just) for binary data; it just ensures the data is sent as-is.) – sideshowbarker Jan 02 '17 at 08:39
  • 1
    What is the character set of `CMD`? *That* is the character set of the arguments to `curl`, i.e. the data, so that is the charset to specify. If you use a file to supply the data, instead of a command-line argument, then you can control the encoding of the file and make it UTF-8. To see `CMD` code page, run command `mode`, e.g. mine says [`Code page: 437`](https://en.wikipedia.org/wiki/Code_page_437). – Andreas Jan 02 '17 at 08:39
  • 1
    To change `CMD` to UTF-8, see [Unicode characters in Windows command line - how?](http://stackoverflow.com/q/388490/5221149) Actually, since that is really the answer for you, I'll vote to close your question as duplicate of that. --- Godt nytår ;-) – Andreas Jan 02 '17 at 08:44
  • If I use --data-binary it is still the same. I also tried to change the codepage to 65001 and set the font to Lucida Console, which seem to make no change. – Adam Westh Jan 02 '17 at 08:55
  • try escaping them with ^ , so it looks like \"^ø^æ^å\" - then realize that microsoft has no idea how to write a terminal and you should not be using cmd.exe, then install a decent terminal, like Cygwin Bash ( https://www.cygwin.com ) – hanshenrik Jan 02 '17 at 17:28

1 Answers1

0

I managed to get this to work by putting the json with Danish characters in its own file, called test.json. I made sure - and this was they key - that the json file was saved as utf-8: I did this by opening the file with notepad and used "Save As", picking utf-8 encoding as file encoding.

At the top of the bat file containing the curl command, I put chcp 65001.

curl.exe -X PUT --header "Content-Type: application/json; charset=UTF-8" --header "Accept: application/json"  -s -d @test.json "<theUrl>"

I added -s to make curl go quiet about an error I was getting (Failed writing body), although the command (which updates a record in a database) appears to run successfully.

user1030520
  • 154
  • 1
  • 13