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

Highlighted
New Member

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 = "https://api.constantcontact.com/ws/customers/" + UserName + "/contacts";


            // Add a new credential for this account

            LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + 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=\"http://www.w3.org/2005/Atom\">";

            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=\"http://ws.constantcontact.com/ns/1.0/\">";

            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=\"http://api.constantcontact.com/ws/customers//lists/2\"" + " />";

            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

            try

            {

            

                // 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);

                streamRequest.Close();

                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

                Reader.Close();

            

                // Close the response to free up the system resources

                Response.Close();

            

                //return XMLResponse;

            }

            catch (WebException err)

            {

                // Get the web exception type and response code

                //return err.ToString();

            }

        }


 


Thanks in Advance!!!!


Glockster.

1 REPLY 1
Highlighted
Moderator

Hi Glockster,   You are

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("https://api.constantcontact.com/ws/customers/" + txtemail.ToString()), "Basic", new NetworkCredential(APIKey + "%" + UserName, Password));


 


Should be:


 


LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + 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.

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured