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

时间:2021-05-30 07:19:17

I want to look at the full URL the HTTParty gem has constructed from my parameters, either before or after it is submitted, it doesn’t matter.

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

I would also be happy grabbing this from the response object, but I can’t see a way to do that either.

我也很乐意从响应对象中获取它,但我也看不到这样做的方法。

(Bit of background)

(背景)

I’m building a wrapper for an API using the HTTParty gem. It’s broadly working, but occasionally I get an unexpected response from the remote site, and I want to dig into why – is it something I’ve sent incorrectly? If so, what? Have I somehow malformed the request? Looking at the raw URL would be good for troubleshooting but I can’t see how.

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

For example:

例如:

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

Presumably generates:

大概产生:

http://example.com/resource?foo=bar

But how can I check this?

但我怎么检查呢?

In one instance I did this:

在一个例子中我这样做:

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

But it didn’t work. Through experimenting I was able to produce this which worked:

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

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

So clearly HTTParty’s default approach to forming the query string didn’t align with the API designers’ preferred format. That’s fine, but it was awkward to figure out exactly what was needed.

很明显,HTTParty构成查询字符串的默认方法与API设计者的首选格式不一致。那很好,但是弄清楚到底需要什么是很尴尬的。

2 个解决方案

#1


19  

You didn't pass the base URI in your example, so it wouldn't work.

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

Correcting that, you can get the entire URL like this:

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

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

Using a class:

使用课程:

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" 

#2


8  

You can see all of the information of the requests HTTParty sends by first setting:

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

class Example
  include HTTParty
  debug_output STDOUT
end

Then it will print the request info, including URL, to the console.

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

#1


19  

You didn't pass the base URI in your example, so it wouldn't work.

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

Correcting that, you can get the entire URL like this:

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

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

Using a class:

使用课程:

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" 

#2


8  

You can see all of the information of the requests HTTParty sends by first setting:

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

class Example
  include HTTParty
  debug_output STDOUT
end

Then it will print the request info, including URL, to the console.

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