dpmp9359 2015-06-05 10:00
浏览 130

Ruby使用Net :: HTTP.post_form向数组发布HTTP请求

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)
  • 写回答

1条回答 默认 最新

  • douyimiao1993 2015-06-05 10:34
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器