When I tried this in the sample code, what I was able to see is that the request parameters (GET) have priority over the POST body. So, I switched to Rack code that handles HTTP requests in Rails. Here is the code from request.rb
Here is the method
- GET - returns request parameters in a hash format
- POST - returns the message body in a hash format
So, according to the code for params , GET parameters should be overridden by POST parameters in case of identical keys. ( self.GET.merge(self.POST) ). But this is contrary to what I got when I tried it in practice.
So, the only chance that this code is overridden by Rails. When I thought about this, it made sense, since the params hash from Rails will always contain the keys "controller" and "action" , which will be absent in the case of Rack. So, I also looked at the Rails code and found that the params method is really overridden. See request.rb and parameters.rb in Rails source code. In parameters.rb we have:
# Returns both GET and POST \parameters in a single hash. def parameters @env["action_dispatch.request.parameters"] ||= begin params = request_parameters.merge(query_parameters) params.merge!(path_parameters) encode_params(params).with_indifferent_access end end alias :params :parameters
and request.rb :
# Override Rack GET method to support indifferent access def GET @env["action_dispatch.request.query_parameters"] ||= (normalize_parameters(super) || {}) end alias :query_parameters :GET # Override Rack POST method to support indifferent access def POST @env["action_dispatch.request.request_parameters"] ||= (normalize_parameters(super) || {}) end alias :request_parameters :POST
So here
- query_parameters - GET alias
- request_parameters - POST method alias
- path_parameters - a method that returns the controller and action for the request as a hash Parameters
- - Alias โโfor
params (it was redefined here)
Note that the GET method and the POST method have also been overridden, mainly to convert the hash returned to the HashWithIndifferentAccess object.
So, looking at the code here ( params = request_parameters.merge(query_parameters) ), it becomes obvious that the POST parameters are overridden by the GET parameters in case of identical keys in Rails. Or, in other words, GET parameters take precedence over POST parameters.
rubyprince
source share