0

I have a Rails API 4.2.10 and a NodeJS API.

Some of the Rails Request are deprecated and needs to be redirected to the NodeJS API.

I would like to not do the redirection by my WebServer (NgInx) but somewhere in Rails.

I would like to redirect the entire request => Url, Headers, body.... And return the NodeJS API body & status code.

Being a noob in ruby I tried some like making the requests myself but the Rails API still uses the create methods & so the related objects for example.

Here is my controller code :

 class Api::V2::RedirectController < ActionController::API

   def apiV3
     redirect_to 'http://api-v3-node/api/v3'   end

 end

When I try with Postman, I get logs but "Could not get any response"

Here are the corresponding outputs :

[2018-09-11T13:08:12.633224 #61]  INFO -- : Started POST "/api/v2/notes?token=[FILTERED]" for 172.19.0.1 at 2018-09-11 13:08:12 +0000
[2018-09-11T13:08:12.656050 #61]  INFO -- : {:_method=>"POST", :_path=>"/api/v2/notes", :_format=>:json, :_controller=>"Api::V2::RedirectController", :_action=>"apiV3", :_status=>302, :_duration=>1.05, :_view=>0.0, :_location=>"http://api-v3-node/api/v3", :short_message=>"[302] POST /api/v2/notes (Api::V2::RedirectController#apiV3)"}
BastienSander
  • 1,718
  • 4
  • 24
  • 50
  • You need to change the Rails action that you want to redirect and use a `redirect_to` pointing to the NodeJS API url. [Here](https://api.rubyonrails.org/classes/ActionController/Redirecting.html) you can find `redirect_to` documentation – Lucas Wieloch Sep 11 '18 at 12:05
  • I am trying with something like ```class Api::V2::RedirectController < Api::BaseController skip_authorization_check def apiV3 redirect_to 'http://XXXX/api/v3' end end ``` But I can't find any success – BastienSander Sep 11 '18 at 12:55
  • That is the way to do it on Rails. What happens after the redirect? What do you get as response? can you post your `BaseController` code? – Lucas Wieloch Sep 11 '18 at 12:59
  • Lots of things, you're right ! I am trying without. As a reponse I don't have anything, the server is not responding even if it has no errors in the console. – BastienSander Sep 11 '18 at 13:02
  • 1
    Not even a status code? That's odd. Can you provide more details? Try posting request log and `BaseController` code. – Lucas Wieloch Sep 11 '18 at 13:06
  • Edited with more infos – BastienSander Sep 11 '18 at 13:14
  • Hmm, everything seems OK on those logs to me. You can see 302 redirect to the location you specified. Which means that if there is an error, it could be on NodeJS application, or `ActionController` (although it's unlikely since logs are OK). Can you perform this action directly on NodeJS API? – Lucas Wieloch Sep 11 '18 at 13:26
  • I think I understand, it seems to be because I redirect to the exact route I wrote but not the path, the query params & the body right ? – BastienSander Sep 11 '18 at 14:32
  • Indeed, yes. I found [this post](https://stackoverflow.com/questions/2144391/ruby-on-rails-post-parameters-on-redirect-to) on the topic, it will point you to the correct solution. – Lucas Wieloch Sep 11 '18 at 15:38

0 Answers0