7

I am implementing a third party API for Shipworks in a Rails server and the Shipworks client app is posting an action param with Shipworks specific semantics.

However the Rails routing logic overwrites this param to be the name of the controller method.

Is there a custom route I could write to get the value of that action param without it being overwritten to be the name of my controller method?

Zabba
  • 64,285
  • 47
  • 179
  • 207
John Wright
  • 2,418
  • 4
  • 29
  • 34

2 Answers2

17

I fell into this trap today and came with this solution in controller method. It's Rails 4.1:

if request.method == "POST"
  action = request.request_parameters['action']
else
  action = request.query_parameters['action']
end
Petr
  • 3,214
  • 18
  • 21
3

I figured it out. There is a raw_post method in AbstractRequest.

So you can do this to parse the raw post params:

def raw_post_to_hash
  request.raw_post.split(/&/).inject({}) do |hash, setting|
    key, val = setting.split(/=/)
    hash[key.to_sym] = val
    hash
   end
end

and then just call raw_post_to_hash[:action] to access the original action param or any other param. There is probably an easier way.

John Wright
  • 2,418
  • 4
  • 29
  • 34