Hi,
I'm using ruby HTTP classes to send POST requests to a server. The
problem is that I'm posting some many-levels hierarchical structures on
my form . When POST parameters are simple key/value there's no
serialization problem with form_data method on PostRequest.
{ :a => :b} => "a=b"
When the values are arrays it works fine too
{ :a => [:b,:c,:d]} => "a[]=b&a[]=c&a[]=d"
But when values are hashes serialization doesn't work as expected.
For example
{ :a => [{:b => :c}, {:d => :e}]} => "a[][b]=c&a[][d]=e"
I don't know if there's any other way to do it but browsing the code it
seems that only one nesting level is supported (maybe is an issue on my
ruby version 1.8.6).
I developed a tiny serialization method. Is a quick and dirty code (the
first release I've made) but it works for me.
module FormSerializer
require "erb"
include ERB::Util
def serialize_form_data(data, path = "",serialized_params = [])
if data.kind_of? Hash
data.each_pair{|k,v| token = (path == "" ? url_encode(k) :
"[#{url_encode(k)}]"); serialize_form_data(v, "#{path}#{token}",
serialized_params)}
elsif data.kind_of? Array
data.each{|v| serialize_form_data(v, "#{path}[]",
serialized_params) }
else
#end of recursion
serialized_params << "#{path}=#{url_encode(data)}"
end
return serialized_params.join("&") if (path == "")
end
#{ :a => :b} = "a=b"
#{ :a => [:b,:c,:d]} = "a[]=b&a[]=c&a[]=d"
#{ :a => :b, :b => :c} = "a=b&b=c"
#{ :a => {:b => :c}} = "a[b] = c"
#{ :a => [{:b => :c}, {:d => :e}]} = "a[][b] = c & a[][d] = e"
end
After this I only needed to modify
def form_data(params, sep = '&')
self.body = serialize_form_data(params)
self.content_type = 'application/x-www-form-urlencoded'
end
I just finished the code some hours ago, basic testing has been
performed.
Hope this will be useful for anyone!
Best regards
Dave Garcia
--
Posted via http://www.ruby-forum.com/.
PS:
On the server side Rails 2.3.2 works fine deserializing my params are :
{"a"=>[{"b"=>"c", "d"=>"e"}], "action"=>"show",
"controller"=>"identities"}
分享到:
相关推荐
Coarse-grained reconfigurable architectures (CGRAs) have drawn increasing attention...on average, as compared with the state-of-the-art methods. The runtime complexity of our approach is also acceptable.
normalize_nested_params ( obj , "contents[][style]" , 'bold' ) # => { "contents" : [ { "style" : "bold" } ] } _ . normalize_nested_params ( obj , "contents[][style]" ) # => 'bold' 在测试用例中查看更...
这里是Spring的一个Nested事务的代码及数据库文件,因为NESTED资源很少,这里作出了一个通俗易懂的 让需要者下载。
We develop a new edge detection algorithm, holistically-nested edge detection (HED), which performs image-to-image prediction by means of a deep learning model that leverages fully convolutional ...
安装将此行添加到您的应用程序的Gemfile中: gem 'nested_form_fields' 然后执行: $ bundle 在您的application.js文件中添加: //= require nested_form_fieldsRails 5.1+ 当Rails从默认堆栈中删除jQuery时,您将...
Weblogic9异常解决nested errors.txtWeblogic9异常解决nested errors.txtWeblogic9异常解决nested errors.txtWeblogic9异常解决nested errors.txtWeblogic9异常解决nested errors.txt
Laravel开发-laravel-nested 向雄辩模型添加嵌套集验证
基于Nested Logit模型的出行路线方式选择和时间价值计算,宗芳,祁文田,本文介绍了Nested Logit模型的效用最大化理论、选择树的建立以及其概率表达式;时间价值的基本计算公式。对人们由长春到吉林的出行路
java解决Handler processing failed; nested exception is java.lang.NoClassDefFoundError
java解决nested exception is java.lang.OutOfMemoryError Java heap space 解决OOM
Laravel开发-eloquent-nested-attributes 嵌套属性允许您通过父级保存关联记录的属性。默认情况下,嵌套属性更新被关闭,您可以使用$nested属性启用它。启用嵌套属性时,将在模型上定义属性编写器。
这是ruby的#values_at哈希方法的扩展版本。 安装 将此行添加到您的应用程序的Gemfile中: gem 'values_at_nested' 然后执行: $ bundle 或将其自己安装为: $ gem install values_at_nested 用法 给定以下哈希值...
Nested VM execution: new direction for KVM.
NULL 博文链接:https://ivan-yan.iteye.com/blog/1007671
Laravel开发-laravel-nestedset Laravel的嵌套集模型
Gson对Java嵌套对象和JSON字符串之间的转换
sqlitejdbc-v037-nested.jar
yii2-nested-sets, Yii框架的嵌套集行为 nest 2的行为 一种利用改进的预排序树遍历算法的Yii框架的现代嵌套。安装安装这个扩展的首选方法是通过 composer插件。运行$ composer require creocoder/yii2-nes
nested fragments nested fragments nested fragments nested fragments
Please use the , vue-draggable-nested-tree will no longer be maintained. 请使用新发布的, vue-draggable-nested-tree 将不再维护. Please use the , vue-draggable-nested-tree will no longer be maintained. ...