HELP! :-) Adding contact via POST returns 400 bad request...Beginner


HELP! :-) Adding contact via POST returns 400 bad request...Beginner

I'm trying to add a contact from a webform using c#.  I can't tell where my issue(s) is/are regarding my 400 errors.  Any help would be VERY much appreciated.


Here's the code:

protected void go_Click(object sender, EventArgs e)


            // Note: Remember to add using System.Net;

            // Create LoginCreditials

            CredentialCache LoginCredentials = new CredentialCache();

            string UserName = "UserName";

            string APIKey = "GUID";

            string Password = "UserPassword";

            string ContactURI = "" + UserName + "/contacts";

            // Add a new credential for this account

            LoginCredentials.Add(new Uri("" + txtemail.ToString()), "Basic", new NetworkCredential(APIKey + "%" + UserName, Password));


            // Create WebRequest

            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(ContactURI);


            // URI for the POST

            // Set Request to be a POST

            Request.Method = "POST";


            // Set the ContentType property.

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


            // Set Request credentials

            Request.Credentials = LoginCredentials;


            // Set up XML String for the POST

            // Long string with quotes, use an absolute string with literals string or a StringBuilder

            string XMLData = "";

            XMLData += "<entry xmlns=\"\">";

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

            //XMLData += "<updated>" + UpdateTimeStamp + "</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=\"\">";

            XMLData += "<EmailAddress>" + txtemail.Text.ToString() + "</EmailAddress>";

            XMLData += "<EmailType>HTML</EmailType>";

            //XMLData += "<FirstName>"&FirstName&"</FirstName>";

            //XMLData += "<LastName>"&LastName&"</LastName>";

            //XMLData += "<PostalCode>"&PostalCode&"</PostalCode>";

            XMLData += "<OptInSource>ACTION_BY_CUSTOMER</OptInSource>";

            XMLData += "<ContactLists>";

            XMLData += "<ContactList id=\"\"" + " />";

            XMLData += "</ContactLists>";

            XMLData += "</Contact>";

            XMLData += "</content>";

            XMLData += "</entry>";


            // Set up the XML Document, application dependant


            // Convert XMLData to byteArray for posting

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


            // Send POST request

            // Recast the response to HttpWebResponse for easier processing

            // Place in a try block to ensure that any errors are caught




                // Set the ContentLength portion of the header

                Request.ContentLength = byteArray.Length;

                string XMLResponse = Convert.ToString(byteArray.Length);


                // Create a stream for the POST Request

                // Note: Remember to add using System.IO

                Stream streamRequest = Request.GetRequestStream();


                // Write the data to the stream.

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


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


                // Process the Response as needed

                // This is a generic StreamReader to read the entire response in

                // You can recast this as any type of stream derivative


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


                // Read the entire XML response to a string

                // Note there may not be a XML response for a successful POST

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


                // Close Reader



                // Close the response to free up the system resources



                //return XMLResponse;


            catch (WebException err)


                // Get the web exception type and response code

                //return err.ToString();




Thanks in Advance!!!!



Hi Glockster,


You are commenting out three required lines in your XML:


            //XMLData += "<updated>" + UpdateTimeStamp + "</updated>";

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

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


Not including the proper fields in your XML will return a 400 error message.  For more information on the minimal fields required for Creates and Updates, you can look here at our Contacts Collection documentation.


I noticed another minor problem in your LoginCredential.Add() statement.  The URI you add should be the Account Base URI.  You would want to use the Username, not the email:


LoginCredentials.Add(new Uri("" + txtemail.ToString()), "Basic", new NetworkCredential(APIKey + "%" + UserName, Password));


Should be:


LoginCredentials.Add(new Uri("" + UserName), "Basic", new NetworkCredential(APIKey + "%" + UserName, Password));


Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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

Visit Page