Error 400 when adding multiple contacts to a list

Regular Participant

Error 400 when adding multiple contacts to a list

 Hi 


I'm trying to add multiple contacts to a list, and for that i had used the documentation found here:


http://developer.constantcontact.com/doc/activities (creating an add contacts/remove contacts activity)


i had used this code but the server send an error 400 message...


 


 


protected function doServerAddContactsToListCall($request,$parameter,$type="POST")


        {


            $ch = curl_init();


            $request = str_replace('http://', 'https://', $request);


            // Convert id URI to BASIC compliant


            curl_setopt($ch, CURLOPT_URL, $request);


            curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");


            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);


            curl_setopt($ch, CURLOPT_USERPWD, $this->requestLogin);


            # curl_setopt ($ch, CURLOPT_FOLLOWLOCATION  ,1);


            curl_setopt($ch, CURLOPT_HEADER, 0);


            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


            curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type:application/x-www-form-urlencoded", 'Content-Length: ' . strlen($parameter)));


            curl_setopt($ch, CURLOPT_FAILONERROR, 1);


            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);     


            curl_setopt($ch, CURLOPT_POST, 1);


            curl_setopt($ch, CURLOPT_POSTFIELDS, $parameter);              


           $emessage = curl_exec($ch);


           if ($this->curl_debug) {   echo $error = curl_error($ch);   }


           curl_close($ch);


           return $emessage;


        }


where $request is : "https://api.constantcontact.com/ws/customers/vali_osf/activities"


$parameter is : "activityType%3DSV_ADD%26data%3DEmail+Address%2CFirst+Name%2CLast+Name%0Ajohn_1_1_2_1%40yahoo.com%2C+John%2C+Miller%0Ajohn_1_1_2_1_3%40yahoo.com%2C+Johny%2C+Boy%0A%26lists%3Dhttp%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fvali_osf%2Flists%2F119%2F2010-10-07" where 119/2010-10-07 is the name of the list (that i allready have)


and $type is :"POST"


I made the conversion to hexa for characters like '/' , '&' , ':', '@', '?', '=' and ';' here :


 


$search  = array('@', ':', '/', ';', '?', '&', '=');


$replace = array('%40', '%3A', '%2F', '%3B', '%3F', '%26', '%3D');          


$string = str_replace($search, $replace, $string);          


where the result of string variable is the same as $parameter.


 


Thanks


 


 


 


 

7 REPLIES 7
Regular Participant

Hello,


After looking at your code, it appears the issue is the last portion of your $parameter. In your example, you use the list name, "119/2010/10/07", as the target list. Our API, however, requires that you designate your target list by list ID (i.e. api.constantcontact.com/ws/customers/vali_osf/lists/2 for your general interests list, etc.) and not by list name.


More information on how to retrieve list information, including list ID, can be found at the following link:


developer.constantcontact.com/doc/contactLists#retrieve_list

Andrew T

Support Engineer, Constant Contact

Regular Participant

 is there a way to retrieve a list by using the list name instead of list id? Thanks

Regular Participant

 nevermind...i understand how to do this, but still i have a problem error 400 when trying to add contacts to a list.


Here is the string that i'm sending in a post variable:


activityType=SV_ADD&data=Email+Address%2CFirst+Name%2CLast+Name%0Ajohn_1_1_2_1@yahoo.com%2C+John


%2C+Miller%0Ajohn_1_1_2_1_3@yahoo.com%2C+Johny%2C+Boy%0A&lists=http%3A%2F%2Fapi.constantcontact.com


%2Fws%2Fcustomers%2Fvali_osf%2Flists%2Fhttp://api.constantcontact.com/ws/customers/vali_osf/lists/21

Employee

There were a few modifications I needed to make in order to make this work. The exact string that worked for me is:


 


activityType=SV_ADD&data=Email+Address%2CFirst+Name%2CLast+Name%0A

john_1_1_2_1%40yahoo.com%2C+John%2C+Miller%0Ajohn_1_1_2_1_3%40yahoo.com%2C+Johny%2C+Boy%0A

&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fvali_osf%2Flists%2F1


 


1. the '@' symbol needs to be URL encoded as %40


 


2. Your lists look like you are attempting to add the contact to two lists, but it does not look like the first one has an actual list id being specified, and the second one is not URL encoded. You currently have:


'&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fvali_osf%2Flists%2Fhttp://api.constantcontact.com/ws/customers/vali_osf/lists/21'


 


I needed to modify this to be:


 


&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fvali_osf%2Flists%2F1


 


After you make these two small modifications, this should successfully create an add contacts activity in your account. I hope this helps. Please let me know if you have any questions or concerns regarding this issue.

David J

Regular Participant

 Now i have the same error 400 when trying to add contacts to a list i have created before.


This is what the function returns:


activityType=SV_ADD&data=Email+Address%2CFirst+Name%2CLast+Name%0Ak30ps%40yahoo.com%2C+


John%2C+Miller%0Asetmefree2u4me%40yahoo.com%2C+Johny%2C+Boy%0A&lists=http%3A%2F%2Fapi.constantcontact.com


%2Fws%2Fcustomers%2Fvali_osf%2Flists%2F32-----The requested URL returned error: 400


In this case i try to add 2 contacts (that allready exists in "General Interest" list) setmefree2u4me@yahoo.com and vali_osf@yahoo.com to the list that has id='32'.


Can you please give me a feedback what i did wrong. Thanks.

Employee

Your request body successfully created an activity in my account, but it added k30ps@yahoo.com and setmefree2u4me@yahoo.com rather than vali_osf@yahoo.com to list id 32. The exact data I posted was:



activityType=SV_ADD&data=Email+Address%2CFirst+Name%2CLast+Name%0A

k30ps%40yahoo.com%2C+John%2C+Miller%0A

setmefree2u4me%40yahoo.com%2C+Johny+Boy%0A

&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fvali_osf%2Flists%2F32



It would seem that the issue lies outside of the request body. Would you be able to reply to this post with the code you are using to make this request? If you would rather send a private email to our webservices support team you can certainly do that instead.


 

David J

Regular Participant

 Meanwhile I found the problem. Consider this topic closed. Thanks.

Developer Portal

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

Visit Page