Faraday ãã©ã¡ã¼ã¿ã®ã¨ã³ã³ã¼ããå·®ãæ¿ãã¦ã¿ã
ååã«å¼ãç¶ãFaraday
ãã¿ã§ãã
Faradayã¯query_stringãescapeããæã«åè§ã¹ãã¼ã¹ã+
ã«escapeãããããªã®ã§
%20
ã«escapeããããã«ERB::Util#url_encode
ã§escapeããããã«ãã¦ã¿ã¾ãã
åè : http://stackoverflow.com/questions/21283414/how-to-avoid-faraday-request-to-encode-get-parameters
ã¾ãã¯æ®éã«ä½¿ã£ã¦ç¢ºèª
åååæ§ã«GETãªã¯ã¨ã¹ãããã¦ã¿ã¾ãã
client = Faraday.new(:url => "http://localhost:4567") do |faraday| faraday.response :logger faraday.adapter Faraday.default_adapter end res = client.get "/test", { "hoge hoge" => "foo bar" }
I, [2014-05-23T22:49:12.358290 #22807] INFO -- : get http://localhost:4567/test?hoge+hoge=foo+bar D, [2014-05-23T22:49:12.358373 #22807] DEBUG -- request: User-Agent: "Faraday v0.9.0" I, [2014-05-23T22:49:12.363857 #22807] INFO -- Status: 200 D, [2014-05-23T22:49:12.363918 #22807] DEBUG -- response: content-type: "application/json" content-length: "23" x-content-type-options: "nosniff" server: "WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)" date: "Fri, 23 May 2014 13:49:12 GMT" connection: "close"
çµæãè¦ã¦ã¿ã㨠hoge+hoge=foo+bar
ã«ãªã£ã¦ã¾ããã
ãã®è¾ºãè¦ãã¨ãXXEncoder
ã£ã¦ããåãã¡ããã¾ãã
ããè¦ãã¨ã
- escape
- unescape
- encode
- decode
ã£ã¦ããã¯ã©ã¹ã¡ã½ãããå®ç¾©ããã¯ã©ã¹ä½ãã°ä½¿ããããªæãããã¾ãã
ä»åã¯ã¨ãããããencode
ã ãå®è£
ããã¯ã©ã¹ãä½ã£ã¦ãã£ã¦ã¿ã¾ãã
class ParamsEncoder def self.encode(params) params.map {|(key, value)| "#{ERB::Util.url_encode(key)}=#{ERB::Util.url_encode(value)}" }.join('&') end def self.to_query end end client = Faraday.new(:url => "http://localhost:4567") do |faraday| faraday.response :logger faraday.adapter Faraday.default_adapter faraday.options.params_encoder = ParamsEncoder end res = client.get "/test", { "hoge hoge" => "foo bar", "bar bar" => "hoge foo" }
I, [2014-05-23T22:56:42.243765 #23036] INFO -- : get http://localhost:4567/test?hoge%20hoge=foo%20bar&bar%20bar=hoge%20foo D, [2014-05-23T22:56:42.243848 #23036] DEBUG -- request: User-Agent: "Faraday v0.9.0" I, [2014-05-23T22:56:42.248266 #23036] INFO -- Status: 200 D, [2014-05-23T22:56:42.248340 #23036] DEBUG -- response: content-type: "application/json" content-length: "44" x-content-type-options: "nosniff" server: "WEBrick/1.3.1 (Ruby/2.1.1/2014-02-24)" date: "Fri, 23 May 2014 13:56:42 GMT" connection: "close"
çµæãã¿ã㨠hoge%20hoge=foo%20bar&bar%20bar=hoge%20foo
ã«ãªãã¾ããã
ãã§ãããã§ããã