1

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

1 Answer 1

1

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.

2
  • Hi. Thank you for your response. After editing the source as above the following error occurs: '#<Net::HTTPOK 200 OK readbody=true>' is not an ActiveModel-compatible object. It must implement :to_partial_path. Oct 22, 2013 at 15:04
  • UPDated answer with appropriate use of render
    – Leger
    Oct 23, 2013 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.