Ruby / Rails Net :: HTTP.post_form嵌套哈希项html转义

时间:2022-07-18 00:13:31

Example I am doing this in rails console:

示例我在rails控制台中执行此操作:

params = {"type"=>["raka"], "fields"=>["exhb_0", "exh0_1", "t_g_a", "hp_1", "s1", "overflade_0", "t2", "t3", "t4"], "railing"=>["A-3"], "wood"=>["wood_6"], "railing_m"=>"0", "order"=>{"sving"=>"right", "size"=>{"ground"=>"123", "floor"=>"6", "a"=>"6", "d"=>"6"}, "comments"=>{"step_2"=>"", "step_3"=>"", "step_4"=>""}, "railing"=>{"1"=>"1", "2"=>"1"}, "railing_m"=>{"1"=>"", "2"=>"", "3"=>"", "4"=>"12"}, "hul"=>{"l"=>"123", "b"=>"123"}, "name"=>"qwed", "email"=>"mail@example.com", "phone"=>"13123", "street"=>"iuuj", "city"=>"ui", "postnr"=>"213"}}

x = Net::HTTP.post_form(URI.parse('http://localhost:3000/download.pdf'), params)

In my Rails console I can see the HTTP post request:

在我的Rails控制台中,我可以看到HTTP post请求:

Started POST "/download.pdf" for 127.0.0.1 at 2013-04-15 16:25:36 +0200
Processing by PublicController#show_pdf as */*
  Parameters: {"type"=>"raka", "fields"=>"t4", "railing"=>"A-3", "wood"=>"wood_6
", "railing_m"=>"0", "order"=>"{\"sving\"=>\"right\", \"size\"=>{\"ground\"=>\"1
23\", \"floor\"=>\"6\", \"a\"=>\"6\", \"d\"=>\"6\"}, \"comments\"=>{\"step_2\"=>
\"\", \"step_3\"=>\"\", \"step_4\"=>\"\"}, \"railing\"=>{\"1\"=>\"1\", \"2\"=>\"
1\"}, \"railing_m\"=>{\"1\"=>\"\", \"2\"=>\"\", \"3\"=>\"\", \"4\"=>\"12\"}, \"h
ul\"=>{\"l\"=>\"123\", \"b\"=>\"123\"}, \"name\"=>\"qwed\", \"email\"=>\"mail@example.com\", \"phone\"=>\"13123\", \"street\"=>\"iuuj\", \"city\"=>\"ui\", \"postn
r\"=>\"213\"}"}

The problem is that all the nested http params are HTML escaped. How do I get rid of that?

问题是所有嵌套的http参数都是HTML转义的。我怎么摆脱它?

2 个解决方案

#1


1  

In Rails world, params is not a regular Hash object that Ruby provides out of the box. In fact its a HashWithIndifferentAccess that is provided by Rails that allows the keys to be accessed, as a symbol or as a string.

在Rails世界中,params不是Ruby提供的常规Hash对象。实际上它是由Rails提供的HashWithIndifferentAccess,允许访问密钥,作为符号或字符串。

irb(main):001:0>params = {"type"=>["raka"], "fields"=>["exhb_0", "exh0_1", "t_g_a", "hp_1", "s1", "overflade_0", "t2", "t3", "t4"], "railing"=>["A-3"], "wood"=>["wood_6"], "railing_m"=>"0", "order"=>{"sving"=>"right", "size"=>{"ground"=>"123", "floor"=>"6", "a"=>"6", "d"=>"6"}, "comments"=>{"step_2"=>"", "step_3"=>"", "step_4"=>""}, "railing"=>{"1"=>"1", "2"=>"1"}, "railing_m"=>{"1"=>"", "2"=>"", "3"=>"", "4"=>"12"}, "hul"=>{"l"=>"123", "b"=>"123"}, "name"=>"qwed", "email"=>"mail@example.com", "phone"=>"13123", "street"=>"iuuj", "city"=>"ui", "postnr"=>"213"}}
irb(main):002:0>params.class
=> Hash
irb(main):003:0>params[:fields]
=> nil
irb(main):004:0>params = params.with_indifferent_access
irb(main):005:0>params.class
=> ActiveSupport::HashWithIndifferentAccess
irb(main):006:0>params[:fields]
=> ["exhb_0", "exh0_1", "t_g_a", "hp_1", "s1", "overflade_0", "t2", "t3", "t4"]

#2


4  

The .post_form method accepts strings, so it causes this escaping problem when passed a nested hash. I had this same problem and switched to the .post method, and solved it.

.post_form方法接受字符串,因此在传递嵌套哈希时会导致此转义问题。我有同样的问题,切换到.post方法,并解决了它。

require "net/http"
uri = URI('http://www.yoururl.com')
http = Net::HTTP.new(uri.host)
response = http.post(uri.path, params.to_query) 

Note also the use of the .to_query method of converting a hash to a string. See here

还要注意使用.to_query方法将哈希转换为字符串。看这里

#1


1  

In Rails world, params is not a regular Hash object that Ruby provides out of the box. In fact its a HashWithIndifferentAccess that is provided by Rails that allows the keys to be accessed, as a symbol or as a string.

在Rails世界中,params不是Ruby提供的常规Hash对象。实际上它是由Rails提供的HashWithIndifferentAccess,允许访问密钥,作为符号或字符串。

irb(main):001:0>params = {"type"=>["raka"], "fields"=>["exhb_0", "exh0_1", "t_g_a", "hp_1", "s1", "overflade_0", "t2", "t3", "t4"], "railing"=>["A-3"], "wood"=>["wood_6"], "railing_m"=>"0", "order"=>{"sving"=>"right", "size"=>{"ground"=>"123", "floor"=>"6", "a"=>"6", "d"=>"6"}, "comments"=>{"step_2"=>"", "step_3"=>"", "step_4"=>""}, "railing"=>{"1"=>"1", "2"=>"1"}, "railing_m"=>{"1"=>"", "2"=>"", "3"=>"", "4"=>"12"}, "hul"=>{"l"=>"123", "b"=>"123"}, "name"=>"qwed", "email"=>"mail@example.com", "phone"=>"13123", "street"=>"iuuj", "city"=>"ui", "postnr"=>"213"}}
irb(main):002:0>params.class
=> Hash
irb(main):003:0>params[:fields]
=> nil
irb(main):004:0>params = params.with_indifferent_access
irb(main):005:0>params.class
=> ActiveSupport::HashWithIndifferentAccess
irb(main):006:0>params[:fields]
=> ["exhb_0", "exh0_1", "t_g_a", "hp_1", "s1", "overflade_0", "t2", "t3", "t4"]

#2


4  

The .post_form method accepts strings, so it causes this escaping problem when passed a nested hash. I had this same problem and switched to the .post method, and solved it.

.post_form方法接受字符串,因此在传递嵌套哈希时会导致此转义问题。我有同样的问题,切换到.post方法,并解决了它。

require "net/http"
uri = URI('http://www.yoururl.com')
http = Net::HTTP.new(uri.host)
response = http.post(uri.path, params.to_query) 

Note also the use of the .to_query method of converting a hash to a string. See here

还要注意使用.to_query方法将哈希转换为字符串。看这里