• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Character encoding of GET request response

Scheduled Pinned Locked Moved Development
3 Posts 2 Posters 3.6k Views 1 Watching
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.
  • M Offline
    MAF1981
    last edited by Mar 11, 2017, 4:00 PM

    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
    • M Offline
      MAF1981
      last edited by MAF1981 Mar 11, 2017, 4:42 PM Mar 11, 2017, 4:36 PM

      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 Offline
        winstongel
        last edited by Aug 21, 2017, 4:50 AM

        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 Sam, technical setup by Karsten.
        This forum is using NodeBB as its core | Contributors
        Contact | Privacy Policy