gpt4 book ai didi

ruby - 我们可以看到使用机械 gem 发送的请求吗?

转载 作者:太空宇宙 更新时间:2023-11-03 17:30:51 25 4
gpt4 key购买 nike

我正在尝试使用 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"

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

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

因此我想首先检查我的请求是否按照我希望的方式发送...

最佳答案

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

require 'mechanize'
require 'logger'

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

运行这段代码会产生:

$ bundle exec ruby ./mech.rbI, [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,identityD, [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.7D, [2016-06-29T13:14:07.019365 #26165] DEBUG -- : request-header: accept-language => en-us,en;q=0.5D, [2016-06-29T13:14:07.019399 #26165] DEBUG -- : request-header: host => google.comI, [2016-06-29T13:14:07.056163 #26165]  INFO -- : status: Net::HTTPMovedPermanently 1.1 301 Moved PermanentlyD, [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-8D, [2016-06-29T13:14:07.056320 #26165] DEBUG -- : response-header: date => Wed, 29 Jun 2016 11:14:07 GMTD, [2016-06-29T13:14:07.056352 #26165] DEBUG -- : response-header: expires => Fri, 29 Jul 2016 11:14:07 GMTD, [2016-06-29T13:14:07.056386 #26165] DEBUG -- : response-header: cache-control => public, max-age=2592000D, [2016-06-29T13:14:07.056456 #26165] DEBUG -- : response-header: server => gwsD, [2016-06-29T13:14:07.056505 #26165] DEBUG -- : response-header: content-length => 219D, [2016-06-29T13:14:07.056536 #26165] DEBUG -- : response-header: x-xss-protection => 1; mode=blockD, [2016-06-29T13:14:07.056568 #26165] DEBUG -- : response-header: x-frame-options => SAMEORIGIND, [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,identityD, [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...

关于ruby - 我们可以看到使用机械 gem 发送的请求吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38095619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com