因为接口内传送的数据太大,所以上面决定要客户端压缩数据后再传给服务器后台,客户端方面是通过 ASIHTTPRequest 来进行的压缩,压缩格式为 gzip.
我想问的是:后端要怎么接收数据呢?或者说 服务器端要做什么配置才能接收到客户端发送来的数据呢?
各位大神有没有什么心得或者经验分享一下呗?先谢过了...... 这个是我写的 middleware 的代码...可是已发送数据就报错了... 能说说应该怎么做吗?
class GzipRequest
def initialize(app)
@app = app
end
def method_handled?(env)
!!(env['REQUEST_METHOD'] =~ /(POST|PUT)/)
end
def encoding_handled?(env)
['gzip', 'deflate'].include? env['HTTP_CONTENT_ENCODING']
end
def call(env)
if method_handled?(env) && encoding_handled?(env)
extracted = decode(env['rack.input'], env['HTTP_CONTENT_ENCODING'])
env.delete('HTTP_CONTENT_ENCODING')
env['CONTENT_LENGTH'] = extracted.length
env['rack.input'] = StringIO.new(extracted)
end
status, headers, response = @app.call(env)
return [status, headers, response]
end
def decode(input, content_encoding)
case content_encoding
when 'gzip' then Zlib::GzipReader.new(input).read
when 'deflate' then Zlib::Inflate.inflate(input.read)
end
end
end
然后报错结果是
[2014-03-02 19:13:29] ERROR ArgumentError: invalid byte sequence in UTF-8
/Users/kintaichi/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.4.5/lib/rack/utils.rb:104:in `normalize_params'
/Users/kintaichi/.rvm/gems/ruby-2.0.0-p247/gems/rack-1.4.5/lib/rack/utils.rb:96:in `block in parse_nested_query'