问题
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?
回答1:
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;
}
}
回答2:
%
is a special character in url's, and is used for url-encoding
You should use another wildcard symbol, like *
http://localhost:3000/search/*
回答3:
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
来源:https://stackoverflow.com/questions/29031526/invalid-request-invalid-http-format-parsing-fails-in-rails