我们可以看到随机械宝石一起发送的请求吗?

Aks*_*s.. 1 ruby redirect get mechanize

我正在尝试使用机械化gem构建GET请求,并且不断收到重定向错误。首先,我想知道是否有一种方法可以查看使用不同参数构建的实际请求,然后再发送?

@agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
cookie = Mechanize::Cookie.new('xyz_session', @tokenid)
cookie.domain = ".mydomain.com"
cookie.path = "/"
@agent.cookie_jar << cookie
@agent.redirection_limit=0
puts @agent.cookies
body = {}.to_json
#@agent.set_proxy("localhost",3000)
@agent.request_headers = {'Content-Type' => "application/json"}
       @agent.get("https://access.test.api.mydomain.com/oidc/v1/user/authorise?response_type=code&redirect_uri=http://localhost&client_id=testclient1&service=AccountSignInService&state=any-state") 
       expect(response.code).to eql(302), "Authorization Code couldn't be received"
Run Code Online (Sandbox Code Playgroud)

我不断 Redirect limit of 0 reached (Mechanize::RedirectLimitReachedError)

如果我没有设置重定向限制,我得到 connection refused: localhost:3000 (Net::HTTP::Persistent::Error)

因此,我想首先检查我的请求是否以我希望的方式发送。

Sta*_*ski 5

只需为代理设置记录器即可获取请求/响应调试。

require 'mechanize'
require 'logger'

agent = Mechanize.new
agent.log = Logger.new(STDERR)
agent.get('http://google.com/')
Run Code Online (Sandbox Code Playgroud)

运行此代码将产生:

$ bundle exec ruby​​ ./mech.rb
I,[2016-06-29T13:14:07.019088#26165] INFO-:Net :: HTTP :: Get:/
D,[2016-06-29T13:14:07.019211#26165]调试-:请求标头:accept-encoding => gzip,deflate,identity
D,[2016-06-29T13:14:07.019247#26165]调试-:请求标头:接受=> * / *
D,[2016-06-29T13:14:07.019281#26165]调试-:请求标题:user-agent => Mechanize / 2.7.4 Ruby / 2.1.5p273(http://github.com/sparklemotion/mechanize /)
D,[2016-06-29T13:14:07.019324#26165]调试-:请求标头:accept-charset => ISO-8859-1,utf-8; q = 0.7,*; q = 0.7
D,[2016-06-29T13:14:07.019365#26165]调试-:请求标题:accept-language => zh-cn,en; q = 0.5
D,[2016-06-29T13:14:07.019399#26165]调试-:请求标题:主机=> google.com
I,[2016-06-29T13:14:07.056163#26165] INFO-:状态:Net :: HTTPMovedPermanently 1.1 301永久移动
D,[2016-06-29T13:14:07.056255#26165]调试-:响应头:位置=> http://www.google.com/
D,[2016-06-29T13:14:07.056289#26165]调试-:响应头:content-type => text / html; 字符集= UTF-8
D,[2016-06-29T13:14:07.056320#26165]调试-:响应标题:日期=>周三,2016年6月29日11:14:07 GMT
D,[2016-06-29T13:14:07.056352#26165]调试-:响应标题:到期=>星期五,2016年7月29日11:14:07 GMT
D,[2016-06-29T13:14:07.056386#26165]调试-:响应头:缓存控制=>公共,最大年龄= 2592000
D,[2016-06-29T13:14:07.056456#26165]调试-:响应头:服务器=> gws
D,[2016-06-29T13:14:07.056505#26165]调试-:响应头:content-length => 219
D,[2016-06-29T13:14:07.056536#26165]调试-:响应头:x-xss-protection => 1; 模式=阻止
D,[2016-06-29T13:14:07.056568#26165]调试-:响应头:x-frame-options => SAMEORIGIN
D,[2016-06-29T13:14:07.056706#26165]调试-:读取219个字节(共219个)
I,[2016-06-29T13:14:07.057585#26165] INFO-:按照重定向至:http://www.google.com/
我[2016-06-29T13:14:07.058406#26165] INFO-:Net :: HTTP :: Get:/
D,[2016-06-29T13:14:07.058454#26165]调试-:请求标头:accept-encoding => gzip,deflate,identity
D,[2016-06-29T13:14:07.058483#26165]调试-:请求标题:接受=> * / *
D,[2016-06-29T13:14:07.058511#26165]调试-:请求标头:user-agent => Mechanize / 2.7.4 Ruby / 2.1.5p273(http://github.com/sparklemotion/mechanize /)
D,[2016-06-29T13:14:07.058540#26165]调试-:请求标头:accept-charset => ISO-8859-1,utf-8; q = 0.7,*; q = 0.7
...