简体   繁体   中英

Nodejs different content-length

I'm downloading an image from internet (from Box), and I'm getting different body.length and header content-length .

The response header content-length is equals to 75739 . But body.length is equals to 72818 .

Now look at this, if I run request(url).pipe(fs.createWriteStream('image.jpg')) , the image got 75739 and open correctly.

Altough, if I run fs.writeFileSync('image.jpg', body) , the image got 137515 and doesn't open correctly.

I also did this gist so you can test by yourself: https://gist.github.com/Daymannovaes/54663e0718f0ded40285d0666f937179

I don't know if encoding is the problem, but I tried to get the byteLength with all the encodes and none of them was to correct size:

length of the body (ascii) 72818 length of the body (utf8) 137515 length of the body (utf16le) 5636 length of the body (ucs2) 145636 length of the body (base64) 54613 length of the body (latin1) 72818 length of the body (binary) 72818 length of the body (hex) 36409

Thanks in advance!

I found the answer. Thanks to a friend of mine. I don't know if is fault of the request library, but if I set { encoding: 'binary' } at the request, everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM