Http - troubles with cyrillic text

Posted on
  • Hi everyone! When i trying send some cyrillic text via using

    require("http").createServer
    

    i getting very strange results.

    Here is my code:

    var wifi = require("Wifi");
    
    wifi.connect('ssid', { password: 'pass'},function(s) {
      console.log(s);
      console.log(wifi.getIP());
    });
    
    function onPageRequest(req, res) {
      var a = url.parse(req.url, true);
      if (a.pathname=="/") {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end('Hello World! Привет Мир!');
      }
    }
    require("http").createServer(onPageRequest).listen(80);
    
    

    And that's what i getting in my browser:

    "Hello World! AFD0C8C2C5D2 ACC8D0!"

    Can someone explain me, what i'm doing wrong? Thanks!

  • This is a constant source of frustration for me. Basically Espruino doesn't directly support unicode since it only has 8 bit characters, so to do anything non-ASCII the IDE replaces anything outside the 0-127 character code range with the escaped utf-8 equivalents before uploading - it's a bit of a hack.

    print('Hello World! Привет Мир!')
    // turns into 
    print('Hello World! \xD0\x9F\xD1\x80\xD0\xB8\xD0\xB2\xD0\xB5\xD1\x82 \xD0\x9C\xD0\xB8\xD1\x80!') 
    

    But I just tried it, and it was broken (it's caused problems ever since it was added to the IDE in 2015!). I've just fixed it and the changes will be in the next IDE release.

    Although even with that doesn't seem to be interpreted correctly by the IDE's terminal. You may have more luck when it's in a webpage, but the foolproof method would be to use the HTML escape codes for the characters you want.

    ... although when it is working, you will need to change Content-Type to text/html; charset=UTF-8 to get anything useful I think.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Http - troubles with cyrillic text

Posted by Avatar for Spyro @Spyro

Actions