Character encoding of GET request response



  • 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!



  • 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,



  • 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.


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.