开发者

Ruby Typhoeus Request: url with quotes

开发者 https://www.devze.com 2023-03-20 23:52 出处:网络
I\'m having a problem doing a request using Typhoeus as my query needs to have quotation marks into it.

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

精彩评论

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