2

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.

Matilda Smeds
  • 1,026
  • 9
  • 17
  • What is the request type you are trying to make (GET/POST/PUT/PATCH)? – MrShemek Mar 21 '19 at 13:43
  • It's a GET request, and I can't really affect that, because there is a round trip to another service. – Matilda Smeds Mar 21 '19 at 13:45
  • The GET request does not have body. You can send JSON as query params or in the headers. Where the JSON is expected (according to the endpoint documentation)? – MrShemek Mar 21 '19 at 13:47
  • Another strange question will follow: Can I do a POST redirect from GET action? Probably not? – Matilda Smeds Mar 21 '19 at 14:01
  • There are two specific issues: 1) the incoming request that needs to be redirected, can be either GET or POST, due to third party implementation. 2) my colleague says it is hard to handle url params in a native app (mobile app), and I don't really know how hard. – Matilda Smeds Mar 21 '19 at 14:03
  • 1
    To answer your question: https://stackoverflow.com/questions/985596/redirect-to-using-post-in-rails – MrShemek Mar 21 '19 at 14:06
  • It looks like GET request can have a body https://stackoverflow.com/questions/978061/http-get-with-request-body, but it is not recommendable. Also could be that Rails disallows this directly. – Matilda Smeds Mar 21 '19 at 14:09

1 Answers1

1

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.

MrShemek
  • 2,275
  • 1
  • 14
  • 19