Nikhil Baliga
Nikhil Baliga

Reputation: 1341

HTTParty Post paramters array of hashes becoming array

I am using HTTParty in Ruby to make a Post call. The server expects to see data in the format:

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

My hash in ruby looks like this

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

I am making this call

class Http
    include HTTParty
end

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

However, the array of hashes is coming incorrectly as

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

Could someone please help me out on this?

Upvotes: 0

Views: 543

Answers (1)

Nikhil Baliga
Nikhil Baliga

Reputation: 1341

I had to pass headers as application/json and pass the params as json by using to_json

Upvotes: 2

Related Questions