Reply
Occasional Participant
csshwetha
Posts: 6
Registered: ‎12-02-2009

The remote server returned an error: (400) Bad Request

Hi, i am using constant contract api for the first time


i am coding in  c# ,this is my code ,i am getting error like this "The remote server returned an error: (400) Bad Request."




            string Etype = "HTML";

         //   string Chr;

            string sUsername = "****";

            string sPassword = "*****";

            string sUri = "https://api.constantcontact.com/ws/customers/" + sUsername + "/activities";

            string sListUri = "http://api.constantcontact.com/ws/customers/" + sUsername + "/lists/1";

            string sAPIKey = "*************";

            Uri address = new Uri(sUri);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

            // HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            request.Credentials = new NetworkCredential((sAPIKey + "%" + sUsername), sPassword);

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";

            StringBuilder data = new StringBuilder();

            data.Append("activityType=" + HttpUtility.UrlEncode("ADD_CONTACTS", Encoding.UTF8));

           // data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,Email Type,First Name,Last Name,Company Name" + Char(10)), Encoding.UTF8));

            data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,Email Type,First Name,Last Name,Company Name" + 10), Encoding.UTF8));

            data.Append(HttpUtility.UrlEncode(( Email + ","+ Etype + "," +Fname + "," + Lname + "," + Org), Encoding.UTF8));





            //data.Append("&data=" + HttpUtility.UrlEncode(("Email Address,Email Type,First Name,Last Name,Company Name" + '\n'), Encoding.UTF8));

            //data.Append(HttpUtility.UrlEncode((txtEmail + ",HTML," + txtFirstName + "," + txtLastName + "," + txtOrganization), Encoding.UTF8));

            data.Append("&lists=" + HttpUtility.UrlEncode(sListUri));



            byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.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());

         }

nvbnvbnvb

DaveBerard
Posts: 1,648
Topics: 7
Kudos: 62
Solutions: 61
Registered: ‎06-19-2008

The error you're receiving

The error you're receiving indicates that your encoded URI is not in the correct format.  The Bulk Ativity collection requires a very specific formatting to seperate all the information.  Here is the encoding strings I use in C# to encode the information.  This can be broken out more, however you do need to make sure that everything is encoded correctly. You will be able to use this in a loop to build the full Bulk Activity request easily as well since you can plug in information as needed.  Please keep in mind, the Bulk Activity is intended to be used when the normal request will be 25 or more updates/creates at a time and not for single contact add activities like a sign up form.  





  1. StringBuilder data = new StringBuilder();



  2. data.Append("activityType=" + HttpUtility.UrlEncode("ADD_CONTACTS", Encoding.UTF8));



  3. data.Append("&data=");



  4. data.Append(HttpUtility.UrlEncode("Email Address,", Encoding.UTF8));



  5. data.Append(HttpUtility.UrlEncode("Email Type,", Encoding.UTF8));



  6. data.Append(HttpUtility.UrlEncode("First Name,", Encoding.UTF8));



  7. data.Append(HttpUtility.UrlEncode("Last Name", Encoding.UTF8));



  8. data.Append(HttpUtility.UrlEncode("\n", Encoding.UTF8));



  9.  



  10. data.Append(HttpUtility.UrlEncode("test1@test.com, ", Encoding.UTF8));



  11. data.Append(HttpUtility.UrlEncode("HTML, ", Encoding.UTF8));



  12. data.Append(HttpUtility.UrlEncode("FNAME, ", Encoding.UTF8));



  13. data.Append(HttpUtility.UrlEncode("LNAME", Encoding.UTF8));



  14.  



  15. data.Append("&lists=");



  16. data.Append(HttpUtility.UrlEncode(sListUri, Encoding.UTF8))



  17.  



  18. byte byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());



  19.  



  20. request.ContentLength = byteData.Length;



  21.  



Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant
csshwetha
Posts: 6
Registered: ‎12-02-2009

data not added to list

thanks for the replay...


i replaced the code  with your code..the code runs without error but data not added to list....i am not getting whats going  with in the code....


this is my new code


 


string Etype = "HTML";

         //   string Chr;

            string sUsername = "****";

            string sPassword = "*****";

            string sUri = "https://api.constantcontact.com/ws/customers/" + sUsername + "/activities";

            string sListUri = "https://api.constantcontact.com/ws/customers/" + sUsername + "/lists/1";

            string sAPIKey = "*******";

            Uri address = new Uri(sUri);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

            // HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

            request.Credentials = new NetworkCredential((sAPIKey + "%" + sUsername), sPassword);

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";



    StringBuilder data = new StringBuilder();

    data.Append("activityType=" + HttpUtility.UrlEncode("ADD_CONTACTS", Encoding.UTF8));

    data.Append("&data=");

    data.Append(HttpUtility.UrlEncode("Email Address,", Encoding.UTF8));

    data.Append(HttpUtility.UrlEncode("Email Type,", Encoding.UTF8));

    data.Append(HttpUtility.UrlEncode("First Name,", Encoding.UTF8));

    data.Append(HttpUtility.UrlEncode("Last Name", Encoding.UTF8));

    data.Append(HttpUtility.UrlEncode("\n", Encoding.UTF8));

    data.Append(HttpUtility.UrlEncode("test@gmail.com ", Encoding.UTF8));

    data.Append(HttpUtility.UrlEncode("HTML, ", Encoding.UTF8));

    data.Append(HttpUtility.UrlEncode("FNAME, ", Encoding.UTF8));

    data.Append(HttpUtility.UrlEncode("Last", Encoding.UTF8));

    data.Append("&lists=");

    data.Append(HttpUtility.UrlEncode(sListUri, Encoding.UTF8));

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

    request.ContentLength = byteData.Length;



           // byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.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());

         }

 


 


 

nvbnvbnvb

DaveBerard
Posts: 1,648
Topics: 7
Kudos: 62
Solutions: 61
Registered: ‎06-19-2008

This may have been a

This may have been a copy/paste error on my part originally, however you are missing a comma in your encoded URI:


 


data.Append(HttpUtility.UrlEncode("test@gmail.com ", Encoding.UTF8));


 


Should be:


 


data.Append(HttpUtility.UrlEncode("test@gmail.com, ", Encoding.UTF8));


 


Otherwise it will try to make an incorrect email address with a space.  What this would result in is a successful activity creation, however the contact would fail to be created since it has an invalid email address (spaces are not allowed in email addresses).  Hope this helps!

Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant
csshwetha
Posts: 6
Registered: ‎12-02-2009

Thanks a lot its working..But

Hi,


    thanks for the reply ,its working now....


 but,the values what we added will not be displayed for the first time,it will be displayed when we enter second values,the second values will not be displayed ,the first values will be displayed....


i am not getting whats going wrong.....

nvbnvbnvb

DaveBerard
Posts: 1,648
Topics: 7
Kudos: 62
Solutions: 61
Registered: ‎06-19-2008

I don't think I completely

I don't think I completely understand what you mean by first tim and second values.  Can you possibly post some detailed requests or information?  If your data is confidential, please send the update to our webservices support team

Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant
csshwetha
Posts: 6
Registered: ‎12-02-2009

the data will not be displayed for the first time

The data wt we enter for the first time will not be displayed in the list ,after entering second time,the first time entered values will be displayed in the list ,second time enterd values will not be displayed in the list,


so i want the way to view the list in my application...


 

nvbnvbnvb

DaveBerard
Posts: 1,648
Topics: 7
Kudos: 62
Solutions: 61
Registered: ‎06-19-2008

If you are using a Bulk

If you are using a Bulk Activity for loading contacts, these are not processed real time.  They we be put in our Bulk Activity job list and processed on a first come, first served queue basis.  This typically takes between 5-10 minutes but may be longer for larger jobs.  If you are looking for instant updates, you will need to use the Contacts collection for single adds/updates. 

Dave Berard
Senior Product Manager, Constant Contact
Jarrad_M
Posts: 1,982
Topics: 276
Kudos: 235
Solutions: 89
Registered: ‎10-18-2010

Re: If you are using a Bulk

This post has been removed or reported as abuse by another community member.