400 Error- Bad Request with c#

cleaks
Regular Participant

400 Error- Bad Request with c#

I've been unable to add a contact to list using c# and the API. I'm debugging in Visual Studio 2008 and I keep getting "Error 400 - Bad Request".This is the code being used and any tips, hint, help would be greatly appreciated.


NetworkCredential nc = new NetworkCredential("APIKey + % + username", "password");

LoginCreds.Add(new Uri("https://api.constantcontact.com/ws/customers/username"),"Basic",nc);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/username/lists");


request.Method = "POST";

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

request.Credentials = LoginCreds;


            string xmlData;

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

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

            xmlData += "<updated>2008-07-23T14:21:06.407Z</updated>";

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

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

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

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

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

            xmlData += "<EmailAddress>test@email.com</EmailAddress>";

            xmlData += "<FirstName>John</FirstName>";

            xmlData += "<LastName>Doe</LastName>";

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

            xmlData += "<ContactLists>";

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

            xmlData += "</ContactLists>";

            xmlData += "</Contact>";

            xmlData += "</content>";

            xmlData += "</entry>";


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

            request.ContentLength = byteArray.Length;

           

            try

            {

                request.ContentLength = byteArray.Length;

                string xmlResponse = "Bytes to send:" + byteArray.Length;



                Stream requestStream = request.GetRequestStream();



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

                requestStream.Close();



            HttpWebResponse response = (HttpWebResponse)request.GetResponse();//stops here



            StreamReader dataStream = new StreamReader(response.GetResponseStream());

           

            xmlResponse += response.StatusCode + response.StatusDescription + dataStream.ReadToEnd();

           

            dataStream.Close();



            response.Close();



            return xmlResponse;

            }

            catch(WebException ex)

            {

                return ex.Message;

            }

2 REPLIES 2
cleaks
Regular Participant

I fixed it.

atran88
Regular Participant

I'm glad you were able to fix your error 400 issue. In case anyone else has this problem, when adding a contact the request should be sent to


https://api.constantcontact.com/ws/customers/username/contacts


In your code example, changing


HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/username/lists");


to


HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/username/contacts");


uploads perfectly.

Andrew T

Support Engineer, Constant Contact

Developer Portal

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

Visit Page