HTTParty Post参数哈希数组成为数组

时间:2014-10-25 15:52:35

标签: ruby httparty

我在Ruby中使用HTTParty进行Post调用。 服务器希望以以下格式查看数据:

{
   "param1": 100,
   "params": [
      {
          "key1": "value1",
          "key2": "value2"
      },
      {
          "key1": "value1",
          "key2": "value2"
      }
   ]
}

我在ruby中的哈希看起来像这样

{
   "param1"=> 100,
   "params"=> [
      {
          "key1"=> "value1",
          "key2"=> "value2"
      },
      {
          "key1"=> "value1",
          "key2"=> "value2"
      }
   ]
}

我正在打这个电话

class Http
    include HTTParty
end

Http.post(url, {:body => my_hash})

但是,哈希数组不正确

{
   "param1": 100,
   "params": [
          "value1",
          "value2",
          "value1",
          "value2"
   ]
}

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

我必须将头文件作为application / json传递,并使用to_json将参数作为json传递