I keep getting a 400 error returned trying to upload to the bulk activities feed. I can upload the text file just fine if I do it manually through the front end
file looks like:
First Name,Last Name,Company Name,Address Line 1,City,State,Zip/Postal Code,Sub Zip/Postal Code,Email Address,Note
First, Last, Company, 1 Main Street, Boston, MA,02118,0293,me@you.com,http://you.com/blahblah
curl:
$ch = curl_init();
$request = str_replace('http://', 'https://', 'https://api.constantcontact.com/ws/customers/USER/activities'); // 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_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
curl_setopt ($ch, CURLOPT_FAILONERROR, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, array('activityType' => 'ADD_CONTACTS', 'dataFile' => '@'.$filename, 'lists' => $listUrl));
$emessage = curl_exec ($ch);
Everything I change just seems to continue giving me a 400 error, been banging my head against a wall all morning trying to get it. Anyone have some insight?
Are you encoding the file
Are you encoding the file contents properly?
I'm not sure if the curl extension does that for you but doubt it.
Basically just use the PHP function urlencode() on the lists URL and the contents of the CSV file you are uploading.
Also it appears to me that your trying to give it the path to a file not the file contents?
eg. 'dataFile' => '@'.$filename
should probably be:
'data' => file_get_contents($filename)
http://developer.constantcontact.com/doc/activities
James Benson, PHP Developer.
Constant Contact Services: http://integrationservic.es/
The 400 error is most likely
The 400 error is most likely due to an error with the request data. With the BULK activities collection you need to make sure that the data is formatted and encoded correctly. Your request data should be formatted like so:
activityType=SV_ADD&data=Email+Address%2CFirst+Name%2CLast+Name%0Apaul%40example.com%2C+Paul%2C+McCartney%0A
john%40example.com%2C+John%2C+Lennon%0A
george%40example.com%2C+George%2C+Harrison%0A
&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fusername%2Flists%2F1
More information can be found here: http://developer.constantcontact.com/doc/activities
Dan H
Support Engineer, Constant Contact
I know the 400 is an error in
I know the 400 is an error in the "request data" but it's so generic that it's pretty much useless to determine what the problem is
I'm not uploading it as urlencoded, it's multipart form. I've gone through the docs already, that's why I'm here. The file will properly upload through the "Import" feature on the front end, so it's clearly in a format that Constant Contact Understands, but I still get a 400 error on upload.
Have you looked into the two
Have you looked into the two suggestions that James provided? I believe that the major problem you're seeing with the upload is that you are not sending the contents of the file but the name of the file. That would result in a 400 bad request.
Dave B Support Engineer, Constant Contact
Yes I have. Except he was
Yes I have. Except he was referring to a different part of the docs, namely the way to transfer application/x-www-form-urlencoded data, not multipart/form-data, which is what I was referring to. @filename is the proper method using php curl to post the file, and the I've tried the file both url encoded and just standard.
is just the generic 400 error all I can get? It can't tell me what is wrong with the file?
oh... and ignore $request = str_replace('http://', 'https://', 'https://api.constantcontact.com/ws/customers/USER/activities'); // Convert id URI to BASIC compliant in my sample. it's a variable replacement in the code that I just pasted in what it ends up as
It works if I delete the two
It works if I delete the two zip code fields and the note field.
Are these not allowed to be uploaded via the bulk upload? are they handled differently?
The Postal Code, Sub Postal
The Postal Code, Sub Postal Code, and Notes fields are allowed in a bulk upload provided that they are spelled correctly. Please see the example below.
activityType=SV_ADD&data=Email+Address%2CFirst+Name%2CPostal+Code%2CSub+Postal+Code%2CNotes%0A
joe%40example4212.com%2CJoe%2C01571%2C2446%2CAdditional+notes%0A
&lists=http%3A%2F%2Fapi.constantcontact.com%2Fws%2Fcustomers%2Fusername%2Flists%2F18
Dan H
Support Engineer, Constant Contact