Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何检查HTTParty生成的完整URL?_Ruby On Rails_Ruby_Httparty - Fatal编程技术网

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

Ruby on rails 如何检查HTTParty生成的完整URL?,ruby-on-rails,ruby,httparty,Ruby On Rails,Ruby,Httparty,我想看看HttpParty gem根据我的参数构建的完整URL,无论是在提交之前还是之后,都无所谓 我也很乐意从response对象中获取这一点,但我也看不到实现这一点的方法 (一点背景) 我正在使用httpartygem为API构建一个包装器。它的工作范围很广,但偶尔我会收到远程站点的意外响应,我想探究原因——是我发送了错误的东西吗?如果是,什么?我是不是把请求的格式弄错了?查看原始URL将有助于进行故障排除,但我不知道如何进行 例如: HTTParty.get('http://example

我想看看HttpParty gem根据我的参数构建的完整URL,无论是在提交之前还是之后,都无所谓

我也很乐意从response对象中获取这一点,但我也看不到实现这一点的方法

(一点背景)

我正在使用httpartygem为API构建一个包装器。它的工作范围很广,但偶尔我会收到远程站点的意外响应,我想探究原因——是我发送了错误的东西吗?如果是,什么?我是不是把请求的格式弄错了?查看原始URL将有助于进行故障排除,但我不知道如何进行

例如:

HTTParty.get('http://example.com/resource', query: { foo: 'bar' })
可能产生:

http://example.com/resource?foo=bar
但是我怎么能检查这个

有一次我这样做了:

HTTParty.get('http://example.com/resource', query: { id_numbers: [1, 2, 3] }
但它不起作用。通过实验,我能够产生这样的效果:

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

因此,很明显,HTTParty默认的查询字符串生成方法与API设计人员首选的格式不一致。这很好,但是很难准确地找出需要什么。

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

纠正这一点,您可以像这样获得整个URL:

res = HTTParty.get('http://example.com/resource', query: { foo: 'bar' })
res.request.last_uri.to_s
# => "http://example.com/resource?foo=bar" 
使用类:

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" 

您可以通过第一个设置查看HTTParty发送的请求的所有信息:

class Example
  include HTTParty
  debug_output STDOUT
end

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

是的,这实际上是从一个子类复制的,因此设置了
base\u uri
。我会改正的。谢谢啊,我明白了!我尝试了
res.last\u uri
,但出现了一个错误。现在我看到,include
.response
可以找到正确的对象。可以工作,但不会显示传递到URL的参数。这非常有用。我将接受@victorkohl提供的答案,因为它更直接适用,但作为调试的一般方法,这是很好的。我得到了一个
NoMethodError:HTTParty:Module
error的未定义方法'debug_output'。那怎么办?