Error 400 - Bad Request

Occasional Participant

Error 400 - Bad Request


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()





                    // Create LoginCreditials

                    CredentialCache LoginCredentials = new CredentialCache();


                    // Add a new credential for this account

                    LoginCredentials.Add(new Uri(""),

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

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


                    string sUri = "";

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




Any suggestions?




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
Occasional Participant

Its working now!

The Sample code really helped.

Thanks Dave!

Developer Portal

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

Visit Page

Register for our "Making Sense of Online Marketing for Retail" Webinar

Join us on Thursday, August 13th at 2pm ET for a free webinar where we'll discuss how to incorporate online marketing into your retail store.

Read More