MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    Character encoding of GET request response

    Development
    2
    3
    2840
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • MAF1981
      MAF1981 last edited by

      Hi there,

      I’m struggling with the wrong character encoding after getting some data back from an Ajax request. For example, the all German umlauts are displayed as 0_1489247969533_1.PNG (correct display has to be: Büro: 20.6°C)
      This is my GET request in the node_helper of my module

      ...
      request({
      			url: encodeURI("http://192.168.178.11/state.cgi?device_id=" + deviceIds),
      			method: 'GET'
      		}, function(error, response, body) {
      			console.log("response: " + response + " - body: " + body);
      			...
      

      The body an XML content and contains the values with the wrong encoding. If I double check the URL within the browser window, the data are returned and displayed with the correct encoding. So it’s not an issue on the server side. Even if I try to set parameter ‘Content-Type’ to ‘application/xml; charset=ISO-8859-1’ or ‘UTF-16’ beneath method: ‘GET’ above it returns the data with the wrong character set.
      Any thoughts on how I can solve this?

      Thanks!

      1 Reply Last reply Reply Quote 0
      • MAF1981
        MAF1981 last edited by MAF1981

        Ok, got it working by myself. Works like charm with iconv-lite. Found the solution here: http://stackoverflow.com/questions/12040643/nodejs-encoding-using-request

        That’s the working code section now:

        var iconv = require('iconv-lite');
        request({
        		encodeURI("http://192.168.178.11/state.cgi?device_id=" + deviceIds),
        		method: 'GET',
        		encoding: null,
        	}, function(error, response, body) {
        		var bodyWithCorrectEncoding = iconv.decode(body, 'iso-8859-1');
        		...
        

        Even the encoding: null is important! Without it does not show the specified encoding.

        BR,

        1 Reply Last reply Reply Quote 1
        • W
          winstongel last edited by

          What is Character Encoding?

          A character encoding tells the computer how to interpret raw zeroes and ones into real characters. It usually does this by pairing numbers with characters. Words and sentences in text are created from characters and these characters are grouped into a character set. Therefore, there must be mappings that describe how to turn each character in a character set into a sequence of bytes. Some characters might be mapped to a single byte but others will have to be mapped to multiple bytes. Those mappings are encoding, because they are telling you how to encode characters into sequences of bytes.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Enjoying MagicMirror? Please consider a donation!
          MagicMirror created by Michael Teeuw.
          Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
          This forum is using NodeBB as its core | Contributors
          Contact | Privacy Policy