Problem while processing HEAD and GET requests in rails 3

We are currently facing the problem of handling HEAD and GET requests. Let me explain the detailed scenario.

Our application integrates incoming and outgoing SMS services.

But from the last 2-3 months, we get 2-3 times a GET request from the SMS service provider, and this affects our system.

After a lengthy discussion with the SMS service provider, they say: "Both Head and Get requests are processed the same way from your end."

I also mentioned this link . You can find relevant magazines in this link.

So can anyone suggest how to solve this problem.

EDIT After research, we found that we get all the parameters in the HEAD and GET requests due to the fact that this server processes it.

+5
source share
2 answers

I think the problem might be ActionDispatch :: Head middleware. Part of this code:

def call(env) if env["REQUEST_METHOD"] == "HEAD" env["REQUEST_METHOD"] = "GET" env["rack.methodoverride.original_method"] = "HEAD" status, headers, _ = @app.call(env) [status, headers, []] else @app.call(env) end end 

Thus, essentially the middleware modifies the request method before the router even receives the request. If you want your router to handle the difference between HEAD and GET requests, you can remove the middleware by adding

 config.middleware.delete "ActionDispatch::Head" 

to your application.rb

Otherwise, you must have access to this variable in your controller as follows:

 if request.env["rack.methodoverride.original_method"]=='HEAD' #do head processing here head :ok, :additional_header => 'value' else #do get processing here end 

If you are worried about performance, I suggest writing your own middleware to handle these requests. Railscasts has some good guides on this .

Also note that other intermediaries, such as Rack :: Cache, may also interfere with this process. Therefore, you should insert your middleware on top:

 config.middleware.insert_before 0, "YourMiddleware" 
+1
source

I would just implement my own: head response as an example at fooobar.com/questions/914842 / ...

 if request.head? head :ok # or whatever else # your other complex stuff here end 

You can also add a route specific to a head request. eg

 match '/validate_messages/sms' => 'validate_messages#noop', via: :head 

and then in your controller

 def noop head :ok end 

basically, you need to implement what you want to do with the HEAD request, otherwise it will continue and use the GET handler

Hope that helps

0
source

Source: https://habr.com/ru/post/1238838/


All Articles