【问题标题】:Ruby/Rails Net::HTTP.post_form nested hash items html escapedRuby/Rails Net::HTTP.post_form 嵌套散列项 html 已转义
【发布时间】:2013-04-15 14:34:34
【问题描述】:

例如我在 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)

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

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\"}"}

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

【问题讨论】:

  • 请描述问题?如果嵌套参数的转义是一个问题,那么您面临什么问题?或者说简单点,为什么要摆脱转义?
  • 问题是嵌套的参数是HTML转义的..
  • 所以?它是否会影响您的程序逻辑?
  • 是的pastie.org/private/jihu4ky7kgnt01k3p4ozg#,如果参数被转义,我不能使用 ruby​​ 样式符号。
  • 参数字段也变了,不再是数组了

标签: ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

.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) 

还要注意使用.to_query 方法将散列转换为字符串。 See here

【讨论】:

    【解决方案2】:

    在 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"]
    

    【讨论】:

    猜你喜欢
    • 2014-06-24
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2012-03-27
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多