Michael Victor Michael Victor - 6 months ago 74
Ruby Question

Sending an Array with HTTParty Gem

I'm using the HTTParty Gem to send data to a SugarCRM API.

I managed to successfully authenticate with the API and now I'm looking to create a lead there, by posting data to the API.

I hit a bump when trying to send the name_value_pair fields though, which are in Sugar CRMs Documentation supposed to be send like this --

"name_value_list" => array(
array(
'name' => 'last_name',
'value' => 'Victor',
),

array(
'name' => 'phone_mobile',
'value' => ‘9999999999’
),
),


Trying to convert this into Ruby, I got this as my request --

response2 = HTTParty.post(base_uri,
body: {
method: "set_entry",
input_type: "JSON",
response_type: "JSON",
rest_data: {
session: session_id,
module_name: 'Leads',
name_value_list:
[
[
'name', 'last_name'
],
[
'value', 'Victor'
],
]
}.to_json
},
debug_output: $stdout
)


However, the CRM API returns me a 500 error. How can I structure this array data so that it mimics the PHP documentation for the
name_value_list
field

Answer

I looked at http://www.w3schools.com/php/php_arrays.asp, at "Associative Arrays", and I think the PHP you have is intending for the key/value pairs to be a hash rather than an array. So I suggest trying this:

name_value_list:
   [
      {
         'name' => 'last_name',
         'value' => 'Victor'
      },
   ]

(The only change is that the inner square brackets are converted to curly braces.)