I am trying to translate the following PHP script into Ruby in order to communicate with a web API. Unfortunately I don't seem to be able to pass the 'recipients' array in the correct way as it is not interpreted correctly by the API. Passing the other parameters works fine though. The problem only appears when passing the recipients array.
How should I format this Array or Hash?
The PHP Script:
<?php
$params=array (
'username' => 'username',
'password' => 'password',
'listId' => 1,
'recipients' =>
array (
0 =>
array (
1 => 'max@mustermann.de',
3 => 'Max',
),
1 =>
array (
1 => 'erika@mustermann.de',
3 => 'Erika',
),
),
'mode' => 'update_add',
'advanced' =>
array (
'optoutSync' => 'global',
'syncFieldId' => 1,
'updateFieldId' => 3,
),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://login.mailingwork.de/webservice/webservice/json/importrecipients');
// curl_setopt($ch, CURLOPT_URL, 'http://ruby-doc.org');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$result = json_decode(curl_exec($ch),true);
print "errorcode:".$result['error'];
print "message:".$result['message'];
print "result:".print_r($result['result'],true);
?>
The Ruby Script:
@params = {
'username' => 'username',
'password' => 'password',
'listId' => 1,
'recipients' => {
0 => { 1 => 'max@mustermann.de', 3 => 'Max' }
#1 => { 1 => 'erika@mustermann.de', 3 => 'Erika2' }
},
'mode' => 'update_add',
'advanced' => {
'optoutSync' => 'global',
'syncFieldId' => 1,
'updateFieldId' => 3
}
}
Net::HTTP.post_form('https://login.mailingwork.de/webservice/webservice/json/importrecipients', @params)
The recipients key can be done like
recipients' => [
0 => { 1 => 'max@mustermann.de', 3 => 'Max' },
1 => { 1 => 'erika@mustermann.de', 3 => 'Erika2' }
], #rest of code goes here
Also don't forget to convert to json with @params.to_json