http - 使用 gzip/deflate 压缩的简单 HTTP 请求

标签 http node.js https gzip deflate

我正在尝试找出轻松发送 HTTP/HTTPS 请求以及处理 gzip/deflate 压缩响应和 cookie 的最佳方式。

我找到的最好的是https://github.com/mikeal/request它处理所有除了压缩。有没有什么模块或方法可以满足我的要求?

如果不是,我可以以某种方式将 request 和 zlib 结合起来吗?我尝试将zlib和http.ServerRequest结合起来,结果惨不忍睹。

最佳答案

对于最近遇到此问题的任何人,请求库现在支持开箱即用的 gzip 解压缩。使用如下:

request(
    { method: 'GET'
    , uri: 'http://www.google.com'
    , gzip: true
    }
  , function (error, response, body) {
      // body is the decompressed response body
      console.log('server encoded the data as: ' + (response.headers['content-encoding'] || 'identity'))
      console.log('the decoded data is: ' + body)
    }
  )

来自 github 自述文件 https://github.com/request/request

gzip - If true, add an Accept-Encoding header to request compressed content encodings from the server (if not already present) and decode supported content encodings in the response. Note: Automatic decoding of the response content is performed on the body data returned through request (both through the request stream and passed to the callback function) but is not performed on the response stream (available from the response event) which is the unmodified http.IncomingMessage object which may contain compressed data. See example below.

关于http - 使用 gzip/deflate 压缩的简单 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8880741/

相关文章:

javascript - MongoDB collection.find() 查询挂起

javascript - 我无法使用 multer 和express 上传图像

node.js - Node 错误: SSL23_GET_SERVER_HELLO:unknown protocol and SSL3_GET_RECORD:wrong version number

java - 如何在 Java 中调用 soap 服务?

http - 从 Es5 中的 Angular2 http 获取错误

java - 将对象放入 ChannelBuffer

javascript - 如何根据文件大小对nodejs中的数组进行排序?

javascript - 在安全 (https) 互联网网站内通过 http 与 LAN 设备进行通信

ssl - Plesk 面板默认 ssl 证书

web-services - Web 应用程序启动时违反 HTTP 协议(protocol)