Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to accept gzipped requests in a rails 5 application?

In the past I've used this solution, but since Rails 5 deprecated ParamsParser middleware, it doesn't work anymore.

like image 747
Diego Plentz Avatar asked Oct 25 '16 16:10

Diego Plentz


3 Answers

Just add:

# config/initializers/middlewares.rb
require 'compressed_requests'

Rails.application.configure do
  config.middleware.insert_after Rack::Sendfile, CompressedRequests
end


# lib/compressed_requests.rb
# Copy the file from the article

You can test it with:

# config/routes.rb
post '/', to: 'welcome#create'

# app/controllers/welcome_controller.rb
class WelcomeController < ActionController::Base
  def create
    render json: params
  end
end

And do the request:

curl --data-binary @<(echo "Uncompressed data" | gzip) \
     -H "CONTENT_ENCODING: gzip" \
     localhost:3000

{"Uncompressed data\n":null,"controller":"welcome","action":"create"}%
like image 188
itsnikolay Avatar answered Nov 17 '22 15:11

itsnikolay


If you have NginX in front of Unicorns, then you can just tell NginX to uncompress the data for you

http://www.pataliebre.net/howto-make-nginx-decompress-a-gzipped-request.html#.WBzSt-ErIUE

https://www.nginx.com/resources/admin-guide/compression-and-decompression/

like image 3
Tilo Avatar answered Nov 17 '22 14:11

Tilo


this should work if you insert the middleware right before Rack::Head

config.middleware.insert_before Rack::Head, "CompressedRequests"

this should do the trick

you can check your app's middleware stack with the following command

rake middleware
like image 3
Rajesh Sharma Avatar answered Nov 17 '22 15:11

Rajesh Sharma