http - Meteor HTTP.get 导致内容损坏(?)

标签 http meteor server

在服务器上,我这样做:

var result = HTTP.get("https://spoticle001.s3-eu-west-1.amazonaws.com/2NtoSkvYZeeetrf3k-0_thumb");
console.log("content length:", result.content.length);

我看到 11781。然而, header “content-length”字段指示 12047,这确实是该 URL 处文件的实际大小。

在客户端,相同的请求导致另一个内容长度:11618。

这似乎是 HTTP.get 最简单的用例。为什么会有如此奇怪的结果?

最佳答案

那是因为 HTTP 使用 npmrequest 模块发出请求,默认情况下,在其中设置了响应编码到 utf8:

encoding - encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer. Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default). (Note: if you expect binary data, you should set encoding: null.)

你可以这样做:

var result = HTTP.get("...", {
  npmRequestOptions: { encoding: null }
});

测试:

result.content.length
> 12047

有用信息:request options .

关于http - Meteor HTTP.get 导致内容损坏(?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46431044/

相关文章:

"email not verified"的 HTTP 状态

javascript - Meteor 使用不同的 Session 和 {{#each}} 来处理点击事件

mongodb - 在不删除或覆盖现有字段的情况下更新 meteor 集合

http - 配置 Nginx 和 Socket.IO

使用具有不同协议(protocol)的代理的 PHP cURL 请求

c# - 发送邮件失败,无法连接到远程服务器

server - 如何在 Google Cloud Compute Engine 中以 root 身份登录?

linux - Tomcat9 在步骤 NAMESPACE 失败

java - HTTP header 变为小写 2

cordova - 在 meteor Cordova 应用程序中播放本地音频