如何检查HTTParty生成的完整URL?

Leo*_*Leo 15 ruby ruby-on-rails httparty

我想查看HTTParty gem从我的参数构建的完整URL,无论是在提交之前还是之后,都没关系.

我也很乐意从响应对象中抓取这个,但我也看不到这样做的方法.

(背景)

我正在使用HTTParty gem为API构建一个包装器.这是广泛的工作,但偶尔我从远程站点得到一个意外的响应,我想深入了解为什么 - 这是我发送错误的东西?如果是这样,什么?我是否以某种方式违反了请求?查看原始URL对于故障排除很有帮助,但我看不出如何.

例如:

HTTParty.get('http://example.com/resource', query: { foo: 'bar' })
Run Code Online (Sandbox Code Playgroud)

大概产生:

http://example.com/resource?foo=bar
Run Code Online (Sandbox Code Playgroud)

但我怎么检查这个?

在一个例子中我这样做:

HTTParty.get('http://example.com/resource', query: { id_numbers: [1, 2, 3] }
Run Code Online (Sandbox Code Playgroud)

但它没有用.通过实验,我能够产生这样的工作:

HTTParty.get('http://example.com/resource', query: { id_numbers: [1, 2, 3].join(',') }
Run Code Online (Sandbox Code Playgroud)

很明显,HTTParty构成查询字符串的默认方法与API设计者的首选格式不一致.这没关系,但确切地知道需要什么是很尴尬的.

vic*_*ohl 23

您没有在示例中传递基URI,因此它不起作用.

更正,您可以像这样获取整个URL:

res = HTTParty.get('http://example.com/resource', query: { foo: 'bar' })
res.request.last_uri.to_s
# => "http://example.com/resource?foo=bar" 
Run Code Online (Sandbox Code Playgroud)

使用课程:

class Example
  include HTTParty
  base_uri 'example.com'

  def resource
    self.class.get("/resource", query: { foo: 'bar' })
  end
end

example = Example.new
res = example.resource
res.request.last_uri.to_s
# => "http://example.com/resource?foo=bar" 
Run Code Online (Sandbox Code Playgroud)


Die*_*zar 9

您可以通过首先设置来查看HTTParty发送的请求的所有信息:

class Example
  include HTTParty
  debug_output STDOUT
end
Run Code Online (Sandbox Code Playgroud)

然后它会将请求信息(包括URL)打印到控制台.