简体   繁体   English

Rails如何遵循HTTParty GET请求中的重定向

[英]Rails how to follow redirect in HTTParty GET request

I am using httparty to send a get request and then trying to follow the redirect: 我正在使用httparty发送get请求,然后尝试遵循重定向:

get 'https://accounts.google.com/o/oauth2/auth'

how can i follow the redirect using HTTParty? 我如何使用HTTParty进行重定向?

sorry if this have been asked before, but i could not find the answer anywhere. 抱歉,如果以前有人问过这个问题,但是我在任何地方都找不到答案。

Thanks 谢谢

Probably it's not needed now, but for someone who need. 可能现在不需要了,但是对于有需要的人来说。

From documentation , there is special option for automatically redirects 文档中可以找到自动重定向的特殊选项

follow_redirects(value = true) ⇒ Object follow_redirects(值= true)⇒对象

Proceed to the location header when an HTTP response dictates a redirect. HTTP响应指示重定向时,转到位置标头。 Redirects are always followed by default. 默认情况下,始终遵循重定向。

So you can use this options like that: 因此,您可以使用以下选项:

HTTParty.get('http://google.com', follow_redirects: true)

Guessing this is a little out of date now, what with the question being about a month old... but my understanding of the HTTParty docs is that it should automatically follow redirects, unless you set the no_follow flag. 现在猜测这有点过时了,问题大约一个月了……但是我对HTTParty文档的理解是,除非您设置了no_follow标志,否则它应该自动跟随重定向。 I haven't successfully tested it though (I'm a total HTTParty n00b and stumbled on this trying to find an answer to my own problem, d'oh...) 我还没有成功测试过(我是一个HTTParty n00b,偶然发现了这个问题,试图找到我自己的问题的答案,d'oh ...)

If that doesn't work, you could always read the header directly and parse the redirect manually. 如果这样不起作用,您总是可以直接读取标头并手动解析重定向。 I've done that in Javascript AJAX requests before, it is not difficult. 之前我已经在Javascript AJAX请求中做到了,这并不困难。 But you shouldn't have to do that with HTTParty. 但是您不必使用HTTParty进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM