cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Formatting Request Payload as an Array rather than a Hash

demontoya
Deputy Chef II
Deputy Chef II

I am trying to format a request payload to be an array of objects, but it appears that the payload HTTP method defaults to formatting it as a hash such as 

{
  email: "exampleemail",
  name: "John Jacob Jingleheimer Schmidt"
}

However, it needs to be enveloped in an array like as follows:

[
  {
    email: "exampleemail",
    name: "John Jacob Jingleheimer Schmidt"
  }
]

The payload HTTP method does not accept arrays, as this will throw an error. Is there a way to do this? What are your thoughts on this? 

1 ACCEPTED SOLUTION

demontoya
Deputy Chef II
Deputy Chef II

Found a solution to this - quite silly now that I think about it.

Since the payload method does not support an array as an argument, I looked at the SDK Reference > HTTP Methods docs yet again, and realized that I could also pass arguments directly within the verb methods such that: 

request_payload = {"data" => {"email" => "exampleemail", "name" => "John Jacob Jingleheimer Schmidt"}}

arrayed_req_payload = [request_payload['data']]

post(url, arrayed_req_payload)

This successfully passed the arrayed payload into the request body. Thank you all for your help and contribution! 

View solution in original post

7 REPLIES 7

Hi @demontoya ,
Could you please do the below modification in your code:

post(url).payload(arrayed_req_payload.to_json).headers("Content-Type" => "application/json")

Please let me know if that worked for you.

Thanks and Regards,
Shivakumara K A

Thank you for the suggestion! This did not work, giving a similar error message as before: undefined method `to_hash' for #<String:0x0...> 

demontoya
Deputy Chef II
Deputy Chef II

Found a solution to this - quite silly now that I think about it.

Since the payload method does not support an array as an argument, I looked at the SDK Reference > HTTP Methods docs yet again, and realized that I could also pass arguments directly within the verb methods such that: 

request_payload = {"data" => {"email" => "exampleemail", "name" => "John Jacob Jingleheimer Schmidt"}}

arrayed_req_payload = [request_payload['data']]

post(url, arrayed_req_payload)

This successfully passed the arrayed payload into the request body. Thank you all for your help and contribution!