我正在查看 Node 的 request模块,文档说回调接受三个参数 - error
、response
(http.IncomingMessage
) 和 body
。
在发出请求时,我发现 body 也可以作为 response
对象的属性使用。
那可靠吗?我可以省略回调中的最后一个参数并使用 response.body
代替吗?
最佳答案
response.body
并不总是与 body
参数相同。如果请求对响应进行任何处理(例如解压缩),则 body
参数将是该处理的结果,但 response.body
可能不会反射(reflect)该处理。您应该使用 response
参数。以下是文档中的一个示例:
For backwards-compatibility, response compression is not supported by default. To accept gzip-compressed responses, set the gzip option to true. Note that the body data passed through request is automatically decompressed while the response object is unmodified and will contain compressed data if the server sent a compressed response.
关于javascript - NodeJS 请求模块 - http.IncomingMessage 上的正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36948231/