Gzip 在 Rails/Passenger/Nginx 中解压 JSON POST 正文
我们的 Rails 代码中有一个接受 JSON POST 正文的函数:(
contacts = ActiveSupport::JSON.decode(request.raw_post.gsub("+", ""))
我知道我也可以从 params["_json"] 获取此内容,但我们有非常大的 (MB)由于某种原因未放入 params["_json"]
的 POST 正文(并且 + 也会引发错误)。
由于 JSON 通常是从移动客户端发送的,因此优化它对于我们来说很重要 。我们要切换到。 但是,无论
我们做什么,我们都会遇到没有行号的相同错误:
MultiJson::DecodeError (743: unexpected token at ''):
我们已经尝试过:
gzipped_contacts = Zlib::GzipReader.new(StringIO.new(request.raw_post)).read
contacts = ActiveSupport::JSON.decode(gzipped_contacts.gsub("+", ""))
这个:
gzipped_contacts = ActiveSupport::Gzip.decompress(request.raw_post)
contacts = ActiveSupport::JSON.decode(gzipped_contacts.gsub("+", ""))
并且在这里找到了解决方案:Rails:如何解压缩压缩的 xml 请求正文?
我很确定这不会发生在控制器级别因为我无法在那里记录任何内容,因此需要在中间件或服务器上完成(但我找不到 Nginx 的任何内容,可以让我们放气)。
We have a function in our Rails code that accepts a JSON POST body:
contacts = ActiveSupport::JSON.decode(request.raw_post.gsub("+", ""))
(I'm aware that I can get this from params["_json"]
as well, but we have extremely large (MBs) POST bodies that do not get put into params["_json"]
for some reason (and + throws errors too).
Since the JSON is usually sent from a mobile client, it's important to us to optimize the upload size. We want to switch to having the POST body gzipped.
However, no matter what we do, we get the same error with no line number:
MultiJson::DecodeError (743: unexpected token at ''):
We have tried:
gzipped_contacts = Zlib::GzipReader.new(StringIO.new(request.raw_post)).read
contacts = ActiveSupport::JSON.decode(gzipped_contacts.gsub("+", ""))
This:
gzipped_contacts = ActiveSupport::Gzip.decompress(request.raw_post)
contacts = ActiveSupport::JSON.decode(gzipped_contacts.gsub("+", ""))
And the solution found here: Rails: how to unzip a compressed xml request body?
I'm pretty sure this is not occurring at the controller level because I can't log anything there, so it needs to be done in the middleware or at the server (but I can't find anything for Nginx that lets us deflate). Please assist!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,事实证明 iPhone 客户端发送了错误的标头。因此,遇到此问题的任何人的解决方案是查看此处的建议:
Rails:如何解压缩压缩的 xml 请求正文?
并验证您正在发送 Content-Type:gzip/json。
Ok, turns out the iPhone client was sending the wrong headers. So the solution for anyone encountering this is to see the advice here:
Rails: how to unzip a compressed xml request body?
And verify that you are sending Content-Type: gzip/json.