Vijikumar M
Vijikumar M

Reputation: 3764

Invalid request: Invalid HTTP format, parsing fails in Rails

While passing a string with special character i am getting Invalid request: Invalid HTTP format, parsing fails error. The error in log as follows.

My Request:

 http://localhost:3000/search/%

Error Log:

 Invalid request: Invalid HTTP format, parsing fails.
/.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/request.rb:84:in `execute'
/.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/request.rb:84:in `parse'
/.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/connection.rb:39:in `receive_data'
/.rvm/gems/ruby-1.9.3-p545/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine'
/.rvm/gems/ruby-1.9.3-p545/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
/.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/backends/base.rb:73:in `start'
/.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/server.rb:162:in `start'
/.rvm/gems/ruby-1.9.3-p545/gems/rack-1.5.2/lib/rack/handler/thin.rb:16:in `run'
/.rvm/gems/ruby-1.9.3-p545/gems/rack-1.5.2/lib/rack/server.rb:264:in `start'
/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands/server.rb:84:in `start'
/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands.rb:76:in `block in <top (required)>'
/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands.rb:71:in `tap'
/.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands.rb:71:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'

What could be the problem? Please suggest me an idea to fix this issue.

How to redirect to another page while getting below error?

enter image description here

Upvotes: 1

Views: 6632

Answers (3)

ala
ala

Reputation: 96

Update your nginx configuration file as below and create 400.html file in public folder.

server { listen 80;

  root /public;   # <--- be sure to point to 'public'!

error_page 400 /400.html;
location = /400.html {
    internal;
}   

}

Upvotes: 1

Sonalkumar sute
Sonalkumar sute

Reputation: 2575

There are number of exceptions which are raised to handle such errors. like ActiveRecord::RecordInvalid ActiveRecord::RecordNotFound. But this are model exceptions. With controller we have ActionController::RoutingError ActionController::BadRequest which are related to routing, mapping url

So when you get 400 error it's a bad request error, So you have to handle it with calling a method which will render an html page in response

Try this

class ApplicationController < ActionController::Base
    rescue_from ActionController::RoutingError, :with => :route_not_found_error
    rescue_from ActionController::BadRequest, :with => :bad_request_error
    resue_from StandardError, :with => :render_server_error

    protected
       def route_not_found_error
          render "shared/404", :status => 404
       end

       def bad_request_error
          render "shared/400", :status => 400
       end

       def render_server_error
          render "shared/500", :status => 500
       end
end

Add pages your 404.html, 400.html, 500.html in app/views/shared

Upvotes: 0

Valdis
Valdis

Reputation: 3178

% is a special character in url's, and is used for url-encoding You should use another wildcard symbol, like * http://localhost:3000/search/*

Upvotes: 1

Related Questions