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

gary1
Executive Chef III
Executive Chef III

I just tested this and the HTTP request payload (specifically for a POST) will accept an array without issue. You may be encoding it incorrectly. Can you provide more details on the error you receive?

I tried enveloping the hash in an array in the following manner:

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

arrayed_req_payload = [request_payload['data']]

post(url).payload(arrayed_req_payload || {})

But I get an error saying undefined method `to_hash' for [nil]:Array at line: xxx where the line is the payload method. It's as if it is trying to convert it to a hash. 

gary1
Executive Chef III
Executive Chef III

Ah, you're in the connector SDK. This is interesting...

Looking at the SDK docs, it does indeed look like it's trying to hash whatever you put inside payload. I recommend checking with support about this. Sorry I couldn't be more help!

No worries! I appreciate you looking into this!