We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

400 Error

chintan_sarda
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
David_J
Employee

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

chintan_sarda
Occasional Participant

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


 

David_J
Employee

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