我正在尝试创建一些简单的Ruby代码来使用Campaign Monitor API添加电子邮件。下面是我的代码。
require 'httparty'
require 'json'
def request
url = 'https://api.createsend.com/api/v3.1/subscribers/MYLISTID.json'
auth = {:username => 'MYAPIKEY', :password => 'x'}
response = HTTParty.post(url,
:basic_auth => auth, :body => {
'EmailAddress' => 'mike@hotmail.com',
'Name' => 'Test',
'Resubscribe' => true,
'RestartSubscriptionBasedAutoresponders' => true
})
puts response
puts response.code
end
request
我可以连接API。然而,当我尝试添加电子邮件时,我得到了以下响应:
{"Code"=>400, "Message"=>"请求反序列化失败。
请检查文档并重试。
字段错误:订阅者"}
400
当我将请求从put
更改为get
时我的回答是:
{"代码"=> 1,"消息"=>"无效的电子邮件地址"}
我不明白我做错了什么,因为我遵循了竞选监控API的文档
看起来一切都设置正确,您只需要将帖子的主体转换为json字符串。
response = HTTParty.post(url,
:basic_auth => auth, :body => {
'EmailAddress' => 'mike@hotmail.com',
'Name' => 'Test',
'Resubscribe' => true,
'RestartSubscriptionBasedAutoresponders' => true
}.to_json)
我想指出一个Campaign Monitor API gem也存在,它将为你做所有的工作。
Campaign Monitor API Gem