02-27-2025 10:28 PM
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?
Solved! Go to Solution.
4 weeks ago
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!
a month ago
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
4 weeks ago
Thank you for the suggestion! This did not work, giving a similar error message as before: undefined method `to_hash' for #<String:0x0...>
4 weeks ago
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!