Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we see the request being sent with mechanise gem?

I am trying to build a GET request with mechanize gem and I keep getting a redirects error. Firstly I want to know if there's a way of seeing the actual request being sent after its built with different params?

@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"

I keep getting Redirect limit of 0 reached (Mechanize::RedirectLimitReachedError)

If I do not set the redirection limit , I get connection refused: localhost:3000 (Net::HTTP::Persistent::Error)

Hence I want to first check if my request is being sent the way I want it to be...

like image 497
Aks.. Avatar asked Sep 14 '25 11:09

Aks..


1 Answers

Just setup logger for agent to get the request/response debug.

require 'mechanize'
require 'logger'

agent = Mechanize.new
agent.log = Logger.new(STDERR)
agent.get('http://google.com/')

running this code will produce:


$ 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] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2016-06-29T13:14:07.019247 #26165] DEBUG -- : request-header: accept => */*
D, [2016-06-29T13:14:07.019281 #26165] DEBUG -- : request-header: user-agent => Mechanize/2.7.4 Ruby/2.1.5p273 (http://github.com/sparklemotion/mechanize/)
D, [2016-06-29T13:14:07.019324 #26165] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
D, [2016-06-29T13:14:07.019365 #26165] DEBUG -- : request-header: accept-language => en-us,en;q=0.5
D, [2016-06-29T13:14:07.019399 #26165] DEBUG -- : request-header: host => google.com
I, [2016-06-29T13:14:07.056163 #26165]  INFO -- : status: Net::HTTPMovedPermanently 1.1 301 Moved Permanently
D, [2016-06-29T13:14:07.056255 #26165] DEBUG -- : response-header: location => http://www.google.com/
D, [2016-06-29T13:14:07.056289 #26165] DEBUG -- : response-header: content-type => text/html; charset=UTF-8
D, [2016-06-29T13:14:07.056320 #26165] DEBUG -- : response-header: date => Wed, 29 Jun 2016 11:14:07 GMT
D, [2016-06-29T13:14:07.056352 #26165] DEBUG -- : response-header: expires => Fri, 29 Jul 2016 11:14:07 GMT
D, [2016-06-29T13:14:07.056386 #26165] DEBUG -- : response-header: cache-control => public, max-age=2592000
D, [2016-06-29T13:14:07.056456 #26165] DEBUG -- : response-header: server => gws
D, [2016-06-29T13:14:07.056505 #26165] DEBUG -- : response-header: content-length => 219
D, [2016-06-29T13:14:07.056536 #26165] DEBUG -- : response-header: x-xss-protection => 1; mode=block
D, [2016-06-29T13:14:07.056568 #26165] DEBUG -- : response-header: x-frame-options => SAMEORIGIN
D, [2016-06-29T13:14:07.056706 #26165] DEBUG -- : Read 219 bytes (219 total)
I, [2016-06-29T13:14:07.057585 #26165]  INFO -- : follow redirect to: http://www.google.com/
I, [2016-06-29T13:14:07.058406 #26165]  INFO -- : Net::HTTP::Get: /
D, [2016-06-29T13:14:07.058454 #26165] DEBUG -- : request-header: accept-encoding => gzip,deflate,identity
D, [2016-06-29T13:14:07.058483 #26165] DEBUG -- : request-header: accept => */*
D, [2016-06-29T13:14:07.058511 #26165] DEBUG -- : request-header: user-agent => Mechanize/2.7.4 Ruby/2.1.5p273 (http://github.com/sparklemotion/mechanize/)
D, [2016-06-29T13:14:07.058540 #26165] DEBUG -- : request-header: accept-charset => ISO-8859-1,utf-8;q=0.7,*;q=0.7
...
like image 76
Stan Brajewski Avatar answered Sep 16 '25 09:09

Stan Brajewski