The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

400 Error

Highlighted
Occasional Participant

400 Error

hi,


i have integrated constant contact api, it was working perfectly but suddenly it has stop working, i am getting 400 error.


In some case i get 409 error,  but when i check constant contact, i cannot found that email entry.


please provide solutions asap, my email id is chintanit22@gmail.com.


attaching code below


 public string createUser(string emailid)

    {

        string sUsername = "USER";

        string sPassword = "PASSWORD";

        string sUri = "https://api.constantcontact.com/ws/customers/" + sUsername + "/contacts";

        string sListUri = "http://api.constantcontact.com/ws/customers/" + sUsername + "/lists/1";

        string sAPIKey = "APIKEY";

        CredentialCache LoginCredentials = new CredentialCache();

        LoginCredentials.Add(new Uri(sUri), "Basic", new NetworkCredential(sAPIKey + "%" + sUsername, sPassword));

        WebRequest Request = WebRequest.Create(new Uri(sUri)); // URI for the GET

        Request.Credentials = LoginCredentials;

        Request.Method = "POST";

        Request.ContentType = "application/atom+xml";

        Request.Credentials = LoginCredentials;

        byte[] byteArray = Encoding.UTF8.GetBytes(getXML(emailid));

        try

        {

            Request.ContentLength = byteArray.Length;

            string XMLResponse = byteArray.Length.ToString();

            Stream streamRequest = Request.GetRequestStream();

            streamRequest.Write(byteArray, 0, byteArray.Length);

            streamRequest.Close();

            HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

            StreamReader Reader = new StreamReader(Response.GetResponseStream());

            XMLResponse = Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();

            Reader.Close();

            Response.Close();

            return "1";

        }

        catch (WebException e)

        {

            if (e.Message.Contains("409"))

            {

                return "0";

            }

            else

            {

                return "-1";

            }

        }

        catch (Exception e)

        {

            if (e.Message.Contains("409"))

            {

                return "0";

            }

            else

            {

                return "-1";

            }

        }

    }

    public string getXML(string emailid)

    {

        string xml = "";

        xml = "<entry xmlns=\"http://www.w3.org/2005/Atom\">";

        xml = xml + "<title type=\"text\"></title>";

        xml = xml + "<updated>" + DateTime.Now.ToString() + "</updated>";

        xml = xml + "<author></author>";

        xml = xml + "<id>data:,none</id>";

        xml = xml + "<summary type=\"text\">Contact</summary>";

        xml = xml + "<content type=\"application/vnd.ctct+xml\">";

        xml = xml + "<Contact xmlns=\"http://ws.constantcontact.com/ns/1.0/\">";

        xml = xml + "<EmailAddress>" + emailid + "</EmailAddress>";

        xml = xml + "<OptInSource>ACTION_BY_CONTACT</OptInSource>";

        xml = xml + "<ContactLists>";

        xml = xml + "<ContactList id=\"http://api.constantcontact.com/ws/customers/USER/lists/1\"></ContactList>";

        xml = xml + "</ContactLists>";

        xml = xml + "</Contact>";

        xml = xml + "</content>";

        xml = xml + "</entry>";

        return xml;

    }

3 REPLIES 3
Highlighted
Employee

Re: 400 Error

Your code looks correct to me, and I was able to add a contact to my account using it. If you are receiving a 409 error this is typically due to a conflict, meaning that the address you are trying to add already exists within your account. I would try to Search for a Contact by Email Address on the address throwing you a 409 error to see where they exist in your account. Please note that once a contact is added to your account, it will always exist as either 'Active', 'Do Not Mail' or 'Removed'. Searching for the email address should let you know exactly where they reside.


 


I hope this helps. If you have any further questions or concerns please let us know. Thanks!

David J

Highlighted
Occasional Participant

400 Error

hi, David Thanx for quick reply,


currently i am not getting 409 error, as i have already catch 409 exception.


i am getting 400 error. it was working fine before few days and suddenly its start giving error.


i have already added 15+ contact, but now it giving error,


please have a look at code again.


 


Thanx and regards,


Chintan Sarda


 

Highlighted
Employee

Re: 400 Error

The only 400 error I can produce from your code is from the line hard-coding list id # 1 ( xml = xml + "<ContactList id=\"http://api.constantcontact.com/ws/customers/USER/lists/1\"></ContactList>";) which is because my account does not have a list with an id of "1". Once I adjust this to a list that exists in my account, I am successfully creating a contact.


 


If you do not have a listid #1 this would also be throwing a 400 error for your as well. If you want to know which lists you have available in your account, you can perform an http get on https://api.constantcontact.com/ws/customers/{user-name}/lists.


 


I am going to send you an email with the full project that I used. If this does not resolve your issue, please reply to that email with your project, preferably including your username and password, and I will be glad to take a closer look at this and provide some more feedback. Thank you.

David J

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured