2015-10-14 40 views
0

我想創建一些簡單的Ruby代碼來使用Campaign Monitor API添加電子郵件。以下是我的代碼。無法將電子郵件添加到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' => '[email protected]', 
         'Name' => 'Test', 
         'Resubscribe' => true, 
         'RestartSubscriptionBasedAutoresponders' => true 
        }) 
    puts response 
    puts response.code 
end 
request 

我可以連接API。但是,當我嘗試添加電子郵件時,我收到以下回復。

{"Code"=>400, "Message"=>"Failed to deserialize your request.
Please check the documentation and try again.
Fields in error: subscriber"}
400

當我更改請求的get代替put 我的迴應是:

{"Code"=>1, "Message"=>"Invalid Email Address"}

我不明白我在做什麼錯了,因爲我按照在Campaign Monitor API

文檔

回答

0

它看起來像你有一切安裝正確,你只需要將帖子的主體轉換成json字符串。

response = HTTParty.post(url, 
    :basic_auth => auth, :body => { 
     'EmailAddress' => '[email protected]', 
     'Name' => 'Test', 
     'Resubscribe' => true, 
     'RestartSubscriptionBasedAutoresponders' => true 
    }.to_json) 

我想指出一個Campaign Monitor API gem也存在,它將爲您完成所有這些工作。

Campaign Monitor API Gem

相關問題