cancel
Showing results for 
Search instead for 
Did you mean: 
Constant Contact wants to help you succeed! We’re celebrating our professional service programs on the Constant Contact Community this month and you have a chance to try one of the services for free! Learn more.

409 error: Adding contact to a new list, while already in another list

Developer

409 error: Adding contact to a new list, while already in another list

Ok, on my website I want ppl to be able to sign up for the a) newsletter and also the b) contest. I'm using the API to add these users to each respective list. Everything is working fine; however, when users try to sign up for the contest, and they already exist in the newsletter list, it gives a 409 error that the user already exists.

 

How do I get the API code to check to see if the user already exists in a list, and then update their subscription so they exist in both lists now?

 

Here is my code:

 

<%

                FirstName= request.form("First_Name") '//Your Contact's First Name

                LastName = request.form("Last_Name") '// Contact First name

                EmailAddress=request.form("Email_Address") '//Your Contact's Email Address

                PhoneNumber=request.form("Phone_Number") '//Your Contact's phone Number

                Comments=request.form("Comments") '//Your Contact's Comments

                UN = "xxxxx" '//Your Account Username (real one used)

                PW = "xxxxxxx" '//Your Account Password (real one used)

                OptInSource="ACTION_BY_CUSTOMER" '//The Actoin Type, usually as listed

                API_Key = "xxxxxxxx" '//Your API Key (real one used)

                List_ID="3" '//Your Particular List ID,,1,2,3 & etc. (Default is 1, first user created would be 2, and so on)

                response.write(PostDataToURL(List_ID,FirstName,LastName,EmailAddress,"POST",UN,PW,API_Key))

                Function PostDataToURL(List_ID,FirstName,LastName,EmailAddress,strMethod,UN,PW,API_Key)

                Dim lngTimeout

                Dim strUserAgentString

                Dim intSslErrorIgnoreFlags

                Dim blnEnableRedirects

                Dim blnEnableHttpsToHttpRedirects

                Dim strHostOverride

                Dim strLogin

                Dim strPassword

                Dim strResponseText

                Dim objWinHttp

                Dim entry

                Dim Base_URL

                Dim UpdateTimeStamp

                Base_URL="https://api.constantcontact.com/ws/customers/"&UN&"/"

                lngTimeout = 59000

                strUserAgentString = "http_requester/0.1"

                intSslErrorIgnoreFlags = 0 ' 13056: ignore all err, 0: accept no err

                blnEnableRedirects = True

                blnEnableHttpsToHttpRedirects = True

                strHostOverride = ""

                strLogin = API_Key & "%" & UN

                strPassword = PW

                UpdateTimeStamp = date()

                strPostData="<entry xmlns=""http://www.w3.org/2005/Atom"">"&vbcrlf&_

                "<title type=""text""> </title>"&vbcrlf&_

                "<updated>"&UpdateTimeStamp&"</updated>"&vbcrlf&_

                "<author></author>"&vbcrlf&_

                "<id>data:,none</id>"&vbcrlf&_

                "<summary type=""text"">Contact</summary>"&vbcrlf&_

                "<content type=""application/vnd.ctct+xml"">"&vbcrlf&_

                "<Contact xmlns=""http://ws.constantcontact.com/ns/1.0/"">"&vbcrlf&_

                "<EmailAddress>"&EmailAddress&"</EmailAddress>"&vbcrlf&_

                "<FirstName>"&FirstName&"</FirstName>"&vbcrlf&_

                "<LastName>"&LastName&"</LastName>"&vbcrlf&_

                "<OptInSource>"&OptInSource&"</OptInSource>"&vbcrlf&_

                "<ContactLists>"&vbcrlf&_

                "<ContactList id="""&Base_URL&"lists/"&List_ID&""" />"&vbcrlf&_

                "</ContactLists>"&vbcrlf&_

                "</Contact>"&vbcrlf&_

                "</content>"&vbcrlf&_

                "</entry>"

                Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")

                objWinHttp.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout

                objWinHttp.Open strMethod,Base_URL&"contacts"

                If strMethod = "POST" Then

                objWinHttp.setRequestHeader "Content-type","application/atom+xml"

                End If

                If strHostOverride <> "" Then

                objWinHttp.SetRequestHeader "Host", strHostOverride

                End If

                objWinHttp.Option(0) = strUserAgentString

                objWinHttp.Option(4) = intSslErrorIgnoreFlags

                objWinHttp.Option(6) = blnEnableRedirects

                objWinHttp.Option(12) = blnEnableHttpsToHttpRedirects

                If (strLogin <> "") And (strPassword <> "") Then

                objWinHttp.SetCredentials strLogin, strPassword, 0

                End If

                On Error Resume Next

                objWinHttp.Send(strPostData)

                If Err.Number = 0 Then                

                PostDataToURL = objWinHttp.ResponseText

                Else

                select case objWinHttp.Status

                case "409"

                PostDataToURL="2"&vbtab&":Contact Already Exists"

                case "201"

                PostDataToURL=""

                case "400"

                'PostDataToURL="0"&vbtab&":Bad Request!! Check Submitted Data: [FirstName='"&FirstName&"',LastName='"&LastName&"',EmailAddress='"&EmailAddress&"']"

                PostDataToURL=""

                case else

                PostDataToURL = "0"&vbtab&":" & objWinHttp.Status & " " &objWinHttp.StatusText

                end select

                End If

                Else

                PostDataToURL = "Error " & Err.Number & " " & Err.Source & " " & _

                Err.Description

                End If

                On Error GoTo 0

                Set objWinHttp = Nothing

                End Function

                %> 

 

Thanks,

 

Bryan

3 REPLIES 3
CTCT Employee

Re: 409 error: Adding contact to a new list, while already in another list

Hi Bryan,

 

I apologize for the delayed response. You would want to first test if the subscriber exists by Searching for a Contact by Email Address. If a subscriber with that email address exists, an entry will be returned in the response XML, meaning that you will need to update the contact with a put, as trying to create a new contact with this email address will cause an error 409. If no entry is returned then you know that the contact does not exist and therefore must be created with a post.

 

While we do have C# code that could be converted to VB.NET that will do this, unfortunately we do not have any example code that performs this in VBScript.

 

Response XML with no entry (contact does not exist)

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://api.constantcontact.com/ws/customers/{username}/contacts</id>
  <title type="text">Contacts for Customer: {username}</title>
  <link href="contacts"></link>
  <link href="contacts" rel="self"></link>
  <author>
    <name>{username}</name>
  </author>
  <updated>2011-04-25T13:25:38.379Z</updated>
  <link href="/ws/customers/{username}/contacts" rel="first"></link>
</feed>

 

 

Response XML with entry (contact already exists)

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://api.constantcontact.com/ws/customers/{username}/contacts</id>
  <title type="text">Contacts for Customer: {username}</title>
  <link href="contacts"></link>
  <link href="contacts" rel="self"></link>
  <author>
    <name>{username}</name>
  </author>
  <updated>2011-04-25T13:27:14.412Z</updated>
  <link href="/ws/customers/{username}/contacts" rel="first"></link>
  <link href="/ws/customers/{username}/contacts?email=test@example.com" rel="current"></link>
  <entry>
    <link href="/ws/customers/{username}/contacts/1" rel="edit"></link>
    <id>http://api.constantcontact.com/ws/customers/{username}/contacts/1</id>
    <title type="text">Contact: test@example.com</title>
    <updated>2011-04-25T13:27:14.448Z</updated>
    <author>
      <name>Constant Contact</name>
    </author>
    <content type="application/vnd.ctct+xml">
      <Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/{username}/contacts/1">
        <Status>Active</Status>
        <EmailAddress>test@example.com</EmailAddress>
        <EmailType>HTML</EmailType>
        <Name>Doe, John</Name>
        <OptInTime>2011-03-14T14:36:36.355Z</OptInTime>
        <OptInSource>ACTION_BY_CONTACT</OptInSource>
        <Confirmed>true</Confirmed>
        <InsertTime>2010-07-23T13:42:52.470Z</InsertTime>
      </Contact>
    </content>
  </entry>
</feed>

David J

Highlighted
Visitor

Re: 409 error: Adding contact to a new list, while already in another list

As far as I can tell, there is an extra step not mentioned above. After looking up the contact's ID using the 'search by email address' request, you then have to download the complete XML <entry> representation of the contact: http://community.constantcontact.com/t5/Documentation/Obtaining-a-Contact-s-Information/ba-p/25057

 

So:

1. Search for contact by email.

2. With the contact ID, retrieve the full <entry> record

3. Send a PUT request to update the contact

 

Three steps simply to add a contact to a list. :(

CTCT Employee

Re: 409 error: Adding contact to a new list, while already in another list

Hey David,

 

I understand that is not ideal but you are correct.

Ryan Davis
Quality Engineer