Answer the question
In order to leave comments, you need to log in
Text encoding error?
The code
const request = require('request'),
fs = require('fs'),
download = require('download');
request('https://litportal.ru/trial/txt/6376244.txt',async (err,res,body)=>{
//res.headers['set-cookie']= 'xf_csrf=eHdNmSDvH01MoWk8; path=/; secure';
let file = fs.writeFileSync('./test.txt',JSON.stringify(res.body).toString(),'utf-8');
console.log(file);
})
Answer the question
In order to leave comments, you need to log in
The request library has been deprecated for a long time. It's better to use axios instead.
You are trying to read a remote file in ANSI encoding and re-encode to UTF-8.
You will need the iconv-lite library for this . Install it:
npm i iconv-lite
Then you can use this code:
const request = require('request'),
fs = require('fs'),
iconv = require('iconv-lite');
request({
url: 'https://litportal.ru/trial/txt/6376244.txt',
encoding: null,
body: 'Buffer'
}).pipe(iconv.decodeStream('win1251'))
.pipe(iconv.encodeStream('utf8'))
.pipe(fs.createWriteStream('file-in-utf8.txt'));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question