Question

I'm building a web app that needs to login to a 3rd party application and then forward the response back to the browser, so it acts like a proxy. We need this because the user must be logged in automatically in both the other app and in our app. Right now I can send the post request to the other app and I have the response, but couldn't figure out how to forward it back to the browser. From what I know, Rails is using render to build the response object automatically.

 def auth_test
    uri = URI 'login_url'

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

    resp = http.post(uri.path, params.to_s)

    logger.info 'Code = ' + resp.code
    logger.info 'Message = ' + resp.message
    resp.each {|key, val| logger.info key + ' = ' + val}
    logger.info "------------------"
    logger.info resp.body

    # if login successful -> login to our app

    # ??? send the response back to the browser (resp) ???

end

Appreciate any help/suggestions.

EDIT

 def auth_test
    uri = URI 'login_url'

    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true

    response = http.post(uri.path, params.to_s)

    logger.info 'Code = ' + response.code
    logger.info 'Message = ' + response.message
    response.each {|key, val| logger.info key + ' = ' + val}
    logger.info "------------------"
    logger.info response.body

    # if login successful -> login to our app

    # ??? send the response back to the browser (resp) ???
    render response
end
Was it helpful?

Solution

Try to look at question re POST-requests and relevant post via Net::HTTP. If you don't use json, just adjust it correspondingly. Then add

render inline: response.body.html_safe, layout: false
  • inline: means you don't need view (template) for controller#auth_test action

  • html_safe tells Rails that your HTML is safe to generate. By default Rails convert any HTML into string to prevent dumb execution of malicious code.

  • layout: false says to render don't use any layout, like 'layouts/application.html.erb' etc.

If needed, you can pass response to View via plain instance variable.

render response is not working, bc Rails try to find associated route for this object in accordance with Rails conventions for render and definitely fails, while it's not ActiveRecord/ActiveModel object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top