Request: So entpacken Sie die gzip-Antwort

Erstellt am 27. Dez. 2013  ·  6Kommentare  ·  Quelle: request/request

Beispiel:

function response (err, response, body) {

// current how to unzip body ?

}
request.get({
    url : path,
    headers: {
        'Accept-Encoding' : 'gzip'
    }
}, response);

Hilfreichster Kommentar

@VRMink hier ist es ein Beispiel

  zlib = require('zlib');

function response (err, res, body) {
//check res header it is gzip
 console.log(res.headers['content-encoding'])
//now body it is gzip stream buffer
      zlib.unzip(body, function(err, buffer) {
             console.log(buffer.toString())
       }

}
    request.get({
        url : apiUrl,
        headers: {
            'Accept-Encoding' : 'gzip',
          },
        encoding : null  // it is very import!!
    }, response);



Alle 6 Kommentare

Ich löse es.

Wie hast du es gelöst?

@VRMink hier ist es ein Beispiel

  zlib = require('zlib');

function response (err, res, body) {
//check res header it is gzip
 console.log(res.headers['content-encoding'])
//now body it is gzip stream buffer
      zlib.unzip(body, function(err, buffer) {
             console.log(buffer.toString())
       }

}
    request.get({
        url : apiUrl,
        headers: {
            'Accept-Encoding' : 'gzip',
          },
        encoding : null  // it is very import!!
    }, response);



Danke - das war nicht ganz offensichtlich :)

@youxiachai Danke!

Danke für deine Lösung!

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen