开发者

Ruby Typhoeus Request: url with quotes

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


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


Try:

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜