cancel
Showing results for 
Search instead for 
Did you mean: 

Error 400 - Bad Request

Highlighted
Occasional Participant

Error 400 - Bad Request

Hi,


I keep hitting this error 400 - Bad Request if I try to add a new list or add a new contact to existing list. I tried the GET function and it worked beautifully so I know there is nothing wrong with the siteurl, username, password and API Key. Please take a look at the code below.


public void AddList()

        {

            {

                try

                {                                  

                    // Create LoginCreditials

                    CredentialCache LoginCredentials = new CredentialCache();

                   

                    // Add a new credential for this account

                    LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/myusername"),

                                                "Basic", new NetworkCredential("xxx-MyKey-xxx" +

                                                "%" + "myusername", "mypassword"));

                   

                    string sUri = "https://api.constantcontact.com/ws/customers/myusername/lists";



                    //Setup an HttpWebRequest to send the data

                    Uri addresss = new Uri(sUri);



                    HttpWebRequest request = WebRequest.Create(addresss) as HttpWebRequest;



                    request.Credentials = LoginCredentials;



                    request.Method = "POST";



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



                    StringBuilder requestParam = new StringBuilder();



                    requestParam.Append("OptInDefault=" + HttpUtility.UrlEncode("false"));

                    requestParam.Append("&Name=" + HttpUtility.UrlEncode("NewList"));

                    requestParam.Append("&ShortName=" + HttpUtility.UrlEncode("NewL"));

                    requestParam.Append("&DisplayOnSignup=" + HttpUtility.UrlEncode("No"));

                    requestParam.Append("&SortOrder=" + HttpUtility.UrlEncode("92"));                  

                   

                    //The response (returned as 'strResponse') will be XML. You can parse this for status messages if you like, or just ignore it.



                    byte[] byteData = UTF8Encoding.UTF8.GetBytes(requestParam.ToString());



                    string st = string.Empty;



                    request.ContentLength = byteData.Length;



                    using (Stream postStream = request.GetRequestStream())

                    {

                        postStream.Write(byteData, 0, byteData.Length);

                    }



                    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)

                    {



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



                        st = reader.ReadToEnd();

                    }

                }

                catch (WebException e)

                {

                    // Get the web exception type and response code

                    MessageBox.Show(e.Status + "With response:" + e.Message);

                }

                catch (Exception e)

                {

                    // Get the exception type

                    MessageBox.Show(e.Message);



                }

            }


Any suggestions?


Thanks,


Kavita

2 REPLIES 2
Highlighted
Moderator

Hi Kavita,   The reason you

Hi Kavita,


 


The reason you are receiving 400 requests is that the data you are sending is not a well formed XML request to add or update a contact.  You can find out more information on how to create the structure of the XML here.  I would recommend checking our our ASP.NET sample code, which is written in C#, instead as it will probably save you alot of time developing.  You can find the sample code here.  

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Occasional Participant

Its working now!

The Sample code really helped.


Thanks Dave!