["raka"], "fields"=>["exhb_0", "exh0_1", "t_g_a", "hp_1", "-6ren">
gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:32:43 25 4
gpt4 key购买 nike

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

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

最佳答案

.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

关于ruby-on-rails - Ruby/Rails Net::HTTP.post_form 嵌套哈希项 html 转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16017884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com