Hamidreza Vakilian
asked on
Nodejs not returning the response body as expected
Hi,
I'm trying to get request this url:
http://www.tsetmc.com/tsev2/data/search.aspx?skey=%D9%88%D8%A8%D8%B5%D8%A7%D8%AF%D8%B1
If you open this url with a browser you get this:
وبصادر,بانك صادرات ايران,28320293733348826,19 3826359786 39373,3312 1247877198 378,531112 6664878711 9,1,1;
but in nodejs I get strange characters; I also tried loading it with a buffer with utf8 encoding but no success.
The response body I get is:
ucU���� A
� �V�� �d��� ���>F�=��d�Vb�,�������y��� ��o��<��%� 9�1�Os�{�O �F��C
ՅP5µ�ʡdްC�VS�NTo*�� 7�m�-�;
�ב2k�"����7vS��0�p�82�F-�y �T�r��Ǚ1
My code is:
Any suggestions?
I'm trying to get request this url:
http://www.tsetmc.com/tsev2/data/search.aspx?skey=%D9%88%D8%A8%D8%B5%D8%A7%D8%AF%D8%B1
If you open this url with a browser you get this:
وبصادر,بانك صادرات ايران,28320293733348826,19
but in nodejs I get strange characters; I also tried loading it with a buffer with utf8 encoding but no success.
The response body I get is:
ucU���� A
� �V�� �d��� ���>F�=��d�Vb�,�������y���
ՅP5µ�ʡdްC�VS�NTo*�� 7�m�-�;
�ב2k�"����7vS��0�p�82�F-�y
My code is:
var http = require("http");
var request = http.get("http://www.tsetmc.com/tsev2/data/search.aspx?skey=%D8%AA%DA%A9%D9%85%D8%A8%D8%A7", function(res){
var data = "";
res.on('data', function(chunk) {
data += chunk;
}).on('end', function() {
console.log(data);
});
});
request.on("error", function(error){
console.error(error.message);
});
Any suggestions?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It was not an encoding issue. The problem is the server sending the http response content in gzip content-encoding format. By default, http library in nodejs doesn't convert the gzipped body, so I had to pipe it to zlib library and the convert it to text.
Thx for this nice solution. Another item on my personal check-list for these encoding issues!
- right to left writing
- character coding
Knowing how to solve all of this Is what you are looking at, but you might consider a more modest target: make it work for your problem.
- I have stopped a long time ago to try solving the fact that displaying some French-accented characters outputs some garbage, AS LONG as the files they are written info have the correct encoding withe the correct characters.
- I agree that getting apparent garbage on a screen is annoying, alarming and painful. However, you know that the ultimate goal of displaying correctly on a web page does work. If you are captuting your (apparent) garbage info a file then examine the gile, is the redult better-looking?