REST Email Marketing API DocumentationExample 2 — Add/update a recipientLet's assume we're creating a preferences center. The user has entered their email address so we can process the add/update operation. We could fetch the list of topics and build the page dynamically, but normally we know the topics we offer and will want to add specific information about each topic, so we won't build that dynamically in this example. For this example, we'll assume the person has given us their first and last name via a web form, and that we've collected the topics the person wants to subscribe to. |
private static void RestExample2() { try { string lastName = "Jones"; string firstName = "Fred"; string url = "http://www.example.com/api/rest/Recipients/AddOne"; string data = "{ 'accountName':'acme','login':'ApiUser','password':'dfb737sdhbu4ew67',"; data += "'topics':['Weekly newsletter'], 'emailAddress':'joe@example.com',"; data += "'demographics':{'first Name':'" + firstName + "', 'last Name':'" + lastName + "'}}"; string results; System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = data.Length; using (Stream webStream = request.GetRequestStream()) using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII)) requestWriter.Write(data); using (System.Net.WebResponse webResponse = request.GetResponse()) using (Stream webStream = webResponse.GetResponseStream()) using (StreamReader responseReader = new StreamReader(webStream)) results = responseReader.ReadToEnd(); if (results == "Recipient added") System.Console.WriteLine("The recipient has been successfully added."); else if (results == "Recipient updated") System.Console.WriteLine("The recipient was already in the system but has been updated."); else if (results == "Invalid email address") //The recipient email address is not valid. Either syntactically invalid or no DNS MX record for the domain. Deal with the error. return; else if (results == "Email address is banned") { // The email address or domain has been banned in the system. Deal with the error. return; } else { System.Console.WriteLine("Some other error occured: {0}", results); return; } //At this point the recipient is valid, either because they were just created, or because //they were already in the system, and they are marked as subscribed to our topic. } catch (System.Exception e) { System.Diagnostics.Debug.Fail(e.Message); } }
# encoding: utf-8 require 'rest-client' require 'json' lastName = "Jones" firstName = "Fred" url = "http://www.example.com/api/rest/Recipients/AddOne" args = {'accountName':'acme','login':'ApiUser','password':'dhd67shd8fgh', 'topics':['Weekly Newsletter'], 'emailAddress':'joe@example.com', 'demographics':{'first Name':firstName, 'last Name':lastName}} resp = RestClient.post url, args.to_json, {"Content-Type": "application/json"} if resp.code != 200 puts "Recipient add failed!" puts resp.code return elsif resp.body == "Recipient added" puts "The recipient has been successfully added." elsif resp.body == "Recipient updated" puts "The recipient was already in the system but has been updated." elsif resp.body == "Invalid email address" #The recipient email address is not valid. Either syntactically invalid or no DNS MX record for the domain. Deal with the error. return elsif resp.body == "Email address is banned" puts "The email address or domain has been banned in the system. Deal with the error." return else puts "Some other error occured." print resp.body return end #At this point the recipient is valid, either because they were just created, or because #they were already in the system, and they are marked as subscribed to our topic.
import requests import json lastName = "Jones" firstName = "Fred" topicBusinessNews = True headers = {'Content-Type': 'application/json'} url = "http://www.example.com/api/rest/Recipients/AddOne" args = {'accountName':'acme','login':'ApiUser','password':'v7vs7yewfjsdv7svd', 'topics':['Business News'], 'emailAddress':'joe@example.com', 'demographics':{'firstname':firstName, 'lastName':lastName}} resp = requests.post(url, json=args, headers=headers) if resp.status_code != 200: print "Recipient add failed!" print resp.status_code return elif resp.text == "Recipient added": #The recipient has been successfully added. elif resp.text == "Recipient updated": #the recipient was already in the system but has been updated. elif resp.text = "Invalid email address": #The recipient email address is not valid. Either syntactically invalid or no DNS MX record for the domain. Deal with the error. return elif resp.text == "Email address is banned": #The email address or domain has been banned in the system. Deal with the error. return else: #some other error occured. Check for the conditions and deal with it. return #At this point the recipient is valid, either because they were just created, or because #they were already in the system, and they are marked as subscribed to our topic.