0

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?

1
  • I'm hesitant to offer a suggestion, as I'm not familiar with HTTParty, but I did find this example. As it appears virtually identical to what you have (the new form of expressing the hash elements is used, but a hash is a hash), I thought this part might be relevant: "Note that "skip_before_filter :verify_authenticity_token" must be set in the "pears" controller for this example". If I'm way off base let me know and I'll delete this comment. Commented Oct 25, 2014 at 20:00

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.