如何使用Rails中的curl编码params?

1 html forms encoding curl ruby-on-rails

我正在使用Pommo作为我的邮件列表,不幸的是Pommo没有提供HTTP API,而是提供和嵌入我可以放在我网站上的订阅表单.但使用该表单发布会让我离开我的网站,这对我来说是不可接受的.

所以我想在Rails中进行字段验证,然后使用curl发布值.

Pommo生成的字段如下所示:

   <input type="text" size="32" maxlength="60" name="Email" id="email" value="" />
   <input type="text" size="32" name="d[1]" id="field1" />
   <input type="text" size="32" name="d[2]" id="field2" />
   <input type="text" size="32" name="d[3]" id="field3" value="México" />
Run Code Online (Sandbox Code Playgroud)

在Rails中生成以下参数:

   { "d"=>{"1"=>"Macario", "2"=>"Ortega", "3"=>"México", "5"=>""}, "Email" => mail@example.com ... }
Run Code Online (Sandbox Code Playgroud)

现在我可以验证电子邮件的格式以及是否存在必需的字段但我不知道如何将此哈希编码为使用curl发布的字符串.

这种数据结构编码是标准的还是依赖于应用程序?它有名字吗?

Sim*_*tti 9

ActiveSupport(在Rails下免费提供)提供了一个方便的to_param方法.

{
  "d" => {
    "1" => "Macario",
    "2" => "Ortega",
    "3" => "Mxico",
    "5" => ""
  },
  "Email" => "mail@example.com"
}.to_param
# => "Email=mail%40example.com&d%5B1%5D=Macario&d%5B2%5D=Ortega&d%5B3%5D=Mxico&d%5B5%5D="
Run Code Online (Sandbox Code Playgroud)