Ruby Typhoeus 请求:带引号的 url

发布于 2024-11-25 02:08:33 字数 425 浏览 2 评论 0原文

我在使用 Typhoeus 执行请求时遇到问题,因为我的查询需要包含引号。

如果 URl 是

url = "http://app.com/method.json?'my_query'"

一切正常。但是,如果查询如下(我已在浏览器中测试过),我尝试运行的方法仅返回我想要的结果:

url2 = "http://app.com/method.json?"my_query""

运行时

Typhoeus::Request.get(url2) 

我得到 (URI::InvalidURIError)

Escapeingquotes with "\" does not工作。我该怎么做?

谢谢

I'm having a problem doing a request using Typhoeus as my query needs to have quotation marks into it.

If the URl is

url = "http://app.com/method.json?'my_query'"

everything works fine. However, the method I'm trying to run only returns the results I want if the query is the following (i've tested it in browser):

url2 = "http://app.com/method.json?"my_query""

When running

Typhoeus::Request.get(url2) 

I get (URI::InvalidURIError)

Escaping quotes with "\" does not work. How can I do this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久而酒知 2024-12-02 02:08:33

您应该使用 正确编码您的 URI URI.encodeCGI.escape,这样做将为您提供正确的 URL,如下所示:

http://app.com/method.json?%27my_query%27 # Single quotes
http://app.com/method.json?%22my_query%22 # Double quotes

You should be properly encoding your URI with URI.encode or CGI.escape, doing so will get you proper URLs like this:

http://app.com/method.json?%27my_query%27 # Single quotes
http://app.com/method.json?%22my_query%22 # Double quotes
牵你手 2024-12-02 02:08:33

尝试:

require 'uri'
URI.encode('"foo"') 
=> "%22foo%22"

Try:

require 'uri'
URI.encode('"foo"') 
=> "%22foo%22"
英雄似剑 2024-12-02 02:08:33

在 GET 请求中传递 json、引号等很棘手。在 Ruby 2+ 中,我们可以使用 Ruby 的 URI 模块的“转义”方法。

> URI.escape('http://app.com/method.json?agent={"account":
{"homePage":"http://demo.my.com","name":"Senior Leadership"}}')

但我建议将其用作 POST 请求并将其作为消息正文传递。

Passing json, quotes etc in GET request is tricky. In Ruby 2+ we can use Ruby's URI module's 'escape' method.

> URI.escape('http://app.com/method.json?agent={"account":
{"homePage":"http://demo.my.com","name":"Senior Leadership"}}')

But I suggest use it as POST request and pass it as a message body.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文