Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an api written in rails which on each request responds with a JSON response.

The response could be huge, so i need to compress the JSON response using gzip.

Wondering how to do this in rails controller?

I have added the line

use Rack::Deflater

in config.ru

Should I also be changing something in the line which renders JSON?

render :json => response.to_json()

Also, how do i check if the response is in gzip format or not..??

I did a curl request from terminal, I see only the normal plain JSON.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

My post Content Compression with Rack::Deflater describes a couple of ways to integrate Rack::Deflater. The easiest would be to just update config/application.rb with:

module YourApp
  class Application < Rails::Application
    config.middleware.use Rack::Deflater
  end
end

and you'll automatically compress all controller responses with deflate / gzip if the client explicitly says they can handle it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...