Search code examples
ruby-on-railsjsonhttp-redirectpayloadactioncontroller

Can Rails action redirect *with* JSON payload?


I have a Rails endpoint that redirects with url parameters.

The code is really simple:

def success_redirect
  redirect_to "#{success_url}/#{encoded_params}"
end

My colleague hopes I could make the same redirect, but send parameters as JSON instead.

Is it possible to do this? Somehow I can't find any resources on the topic, which seems unusual.


Solution

  • Short answer - it is not possible.

    When you are using redirect_to, Rails sets status header to 302 and location header with to the URL you want to reach. Then, the browser interprets this response and redirects you.

    Also, for GET request, it is possible (IMO, not recommended) to include a body (your JSON payload). However, not in Rails.