从 Rails 控制器发送 POST 请求进行身份验证
我对 Ruby 和 RoR 还很陌生,一直在努力解决一个问题,但没有取得任何进展。
基本上我正在构建一个“代理”网络服务来处理某些请求,并将它们传递给第三方网站。从第三方网站收到的响应采用 HTML 格式,然后将对其进行解析并给出适当的 XML 响应。
我现在需要通过 Rails 中的控制器发送 POST 请求来验证用户身份,我正在使用以下代码执行此操作:
require "net/http"
require "uri"
uri = URI.parse("http://myurl.com/members.cgi")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"email_login" => "[email protected]", "password_login" => "password"})
response = http.request(request)
我的问题在于我收到的响应。此特定 POST 请求在成功时(即用户已成功通过身份验证)将返回 302 重定向。这本身不是问题,因为我可以通过获取“位置”标头值来跟踪重定向:
redirectLocation = response['location']
我遇到的问题是在使用以下行进行重定向时保持自己的身份验证:
redirectResponse = Net::HTTP.get_response(URI.parse(redirectLocation))
这遵循重定向但返回的响应显示我没有经过身份验证??
我真的不明白为什么会发生这种情况。我可以看到我的原始响应返回了一个身份验证 cookie:
response['cookie']
所以最后我的问题是我需要做什么才能让服务器识别我的身份验证状态?以某种方式通过我的第二个请求传递 cookie?如果是这样,我该怎么做?
非常感谢您抽出时间!
罗格
I am quite new to Ruby and RoR, and have been struggling with a problem and am not getting anywhere.
Basically I am building a "proxy" webservice that will handle certain requests, passing them to a third party website. The response received from the third party website is in HTML, which will then be parsed and an appropriate XML response will be given.
I'm at a point where I need to send POST requests via my controller in Rails to authenticate a user, which I'm doing using this code:
require "net/http"
require "uri"
uri = URI.parse("http://myurl.com/members.cgi")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"email_login" => "[email protected]", "password_login" => "password"})
response = http.request(request)
My problem lies on the response I am receiving. This specific POST request, when successful (i.e. user has successfully authenticated), is returning a 302 redirect. That in itself is not an issue, as I can follow the redirect by getting the 'location' header value:
redirectLocation = response['location']
Where I'm getting stuck is in keeping myself authenticated when following the redirect with the following line:
redirectResponse = Net::HTTP.get_response(URI.parse(redirectLocation))
This follows the redirect but return a response showing I am not authenticated??
I don't really understanding why this is happening. I can see there is an authentication cookie being returned with my original response by reading:
response['cookie']
So finally my question is what do I need to do to get the server to recognise my authentication status? Pass the cookie with my second request somehow? If so, how do I do it?
Many thanks in advance for your time!
Rog
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您需要设置 cookie。我认为它可能会给你某种会话 ID。您需要在每个请求中传递它。
查看 this 代码片段,了解如何传递您获得的 cookie对您的新请求的答复。
Yes you need to set the cookie. I think it probably give you some kind of a session id. Which you need to pass with every request.
look at this code snippet for a example on how to pass on a cookie that you get as a response with your new requests.