400 Error when POSTing a new contact

SOLVED
Go to solution
SteveG80041
Participant

400 Error when POSTing a new contact

I was able to successfully GET then PUT a record, but when trying to POST a new record I keep getting a 400 error.

 

URL: https://api.constantcontact.com/ws/customers/steve@shahcorp.com/contacts

Content-Type:application/atom+xml

XML Content:

 

<entry xmlns="http://www.w3.org/2005/Atom">
      <id>urn:uuid:E8553C09F4xcvxCCC53F481214230867087</id>
      <title type="text">Contact: john.doe@mail.com</title>
      <updated>2011-06-01T18:20:11.000Z</updated>
      <author>
            <name>CBS</name>
      </author>
      <summary type="text">Contact</summary>
      <content type="application/vnd.ctct+xml">
            <Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="urn:uuid:E8553C09F4xcvxCCC53F481214230867087">
                  <OptInSource>ACTION_BY_CUSTOMER</OptInSource>
                  <EmailAddress>john.doe@mail.com</EmailAddress>
                  <FirstName>John</FirstName>
                  <LastName>Doe</LastName>
            </Contact>
      </content>
</entry>

 

Any ideas what I could be doing wrong?

1 ACCEPTED SOLUTION
bsoder
Employee

Hello,

 

I took a look at your XML and I see that you are not including a contact list to add the contact to.  I believe this is the only reason your XML is not posting successfully.

 

You would want to add a contact list inside the contact node like this:

<ContactLists>
     <ContactList id="http://api.constantcontact.com/ws/customers/joesflowers/lists/1" />
</ContactLists>


The Contact List ID can be found by performing a GET call on the Contact Lists URI.

 

If you still have any questions please let me know.

Benjamin Soder
NOC Analyst
Constant Contact

View solution in original post

9 REPLIES 9
bsoder
Employee

Hello,

 

I took a look at your XML and I see that you are not including a contact list to add the contact to.  I believe this is the only reason your XML is not posting successfully.

 

You would want to add a contact list inside the contact node like this:

<ContactLists>
     <ContactList id="http://api.constantcontact.com/ws/customers/joesflowers/lists/1" />
</ContactLists>


The Contact List ID can be found by performing a GET call on the Contact Lists URI.

 

If you still have any questions please let me know.

Benjamin Soder
NOC Analyst
Constant Contact

View solution in original post

It actually worked with the contact list. I swear I tried with and without the contact list multiple times with no luck earlier.

 

Thanks.

RichardN3
Regular Participant

I am having the same issue which I cannot resolve. My list field is blank:

 

include_once('constant_contact_class.php');
			$ccContactOBJ = new CC_Contact();
			$ccListOBJ = new CC_List(); 
			$disabled = null;
			
			//get all available lists for the current CC account.
			$allLists = $ccListOBJ->getLists(); //die(var_dump($allLists));
			//get all available states
			//$allStates = $ccContactOBJ->getStates();
			//get all available countries
			//$allCountries = $ccContactOBJ->getCountries();
	
			$postFields = array();
			$postFields["email_address"] = $email;
			$postFields["first_name"] = $name;
			$postFields["company_name"] = $company;
			

// SOMEHOW THIS ADDS LIST INFO TO THE XML (WHEN UNCOMMENTED), BUT STILL GET ERROR
//$postFields["lists"] = $allLists[0]; $contactXML = $ccContactOBJ->createContactXML(null,$postFields); //die(var_dump($contactXML)); //if (!$ccContactOBJ->addSubscriber($contactXML)) die($contactXML);

 I am getting Error 400 if I uncomment this:

if (!$ccContactOBJ->addSubscriber($contactXML))

If you do not specify valid Contact Lists to add your Contact to, you will correctly receive a 400 error back telling that you must specify a valid Contact List to add them to.  This is required to create Contacts.

Dave Berard
Senior Product Manager, Constant Contact
RichardN3
Regular Participant

Hi Dave

 

Thanks for the reply.

 

I thought I had specified it correctly though.

 

In the class I have put the name of it in the array - it is the only element in the array and this is working as if I var_dump the variable it shows me the list name, ID and URL

Would it be possible for you to provide the value of $contactXml here so we can look at the raw data (please comment out anything that would personally identify your account or personal information).  Without the lists being added, which you currently have commented out, I would expect that to generate a 400 error.  With it uncommented, I have a suspicion as to why it would generate an error but I'd like to see the raw XML before I guess.

Dave Berard
Senior Product Manager, Constant Contact
RichardN3
Regular Participant

Hi thanks for the response. Please see below:

 

COMMENTED OUT:

<entry xmlns="http://www.w3.org/2005/Atom">
<title>TitleNode</title>
<updated>2012-02-17T13:02:48+01:00</updated>
<author>
<name>CTCT Samples</name>
</author>
<id>urn:uuid:E8553C09F4xcvxCCC53F481214230867087</id>
</entry>
</p>
<summary type="text">Customer document</summary>
<content type="application/vnd.ctct+xml">
<contact xmlns="http://ws.constantcontact.com/ns/1.0"></contact>
Customer document
<emailaddress>test@test.com</emailaddress>
<firstname></firstname>
<lastname></lastname>
<middlename></middlename>
<optinsource>ACTION_BY_CONTACT</optinsource>
<homephone></homephone>
<addr1></addr1>
<addr2></addr2>
<addr3></addr3>
<city></city>
<statecode></statecode>
<statename></statename>
<countrycode></countrycode>
<postalcode></postalcode>
<subpostalcode></subpostalcode>
<emailtype></emailtype>
<contactlists></contactlists>
</content>


FORCED LIST ( $postFields["lists"] = $allLists[0];   uncommented)

<entry xmlns="http://www.w3.org/2005/Atom">
</p>
<summary type="text">Customer document</summary>
<content type="application/vnd.ctct+xml">
<contact xmlns="http://ws.constantcontact.com/ns/1.0"></contact>
Customer document
<emailaddress>test@test.com</emailaddress>
<firstname></firstname>
<lastname></lastname>
<middlename></middlename>
<optinsource>ACTION_BY_CONTACT</optinsource>
<homephone></homephone>
<addr1></addr1>
<addr2></addr2>
<addr3></addr3>
<city></city>
<statecode></statecode>
<statename></statename>
<countrycode></countrycode>
<postalcode></postalcode>
<subpostalcode></subpostalcode>
<emailtype></emailtype>
<contactlists>
<contactlist id="http://api.constantcontact.com/ws/customers/username/lists/72"></contactlist>
<contactlist id="Newsletter" 2012="" sign-ups="" website=""></contactlist>
</contactlists>
</content>


Incase it helps here is class variables:

 

// FROM HERE YOU MAY MODIFY YOUR CREDENTIALS        
        var $login = 'myusername(hidden)'; //Username for your account
        var $password = 'mypassword(hidden)'; //Password for your account
        var $apikey = 'my-api-key(hidden)'; // API Key for your account.
        
        // CONTACT LIST OPTIONS
        var $contact_lists = array('Newsletter Website Sign-ups 2012'); // Define which lists will be available for sign-up.
        var $force_lists = true; // Set this to true to take away the ability for users to select and de-select lists
        var $show_contact_lists = false; // Set this to false to hide the list name(s) on the sign-up form.
        // NOTE - Contact Lists will only be hidden if force_lists is set to true. This is to prevent available checkboxes form being hidden.

 

Hey Richard,

 

It looks like you are using our cc_class.php file as a library.  This is not really meant to be used as a library but we do have a very user friendly php library under our code samples which you can find here.  The code you can use with our new wrapper would be something like this. 

 

<?php 
/****************************************************************************************
 * 
 * 		Please note that this is just an example, please change the credentials to
 * 		your own constant contact login credentials.  The following code is an example of:
 * 
 * 		Search for contact by email address
 * 		Creating a new contact if it doesen't already exist
 * 		If Contact exists, then update the contact by adding it to a new list
 * 		
 ****************************************************************************************/

// this application uses our new wrapper that can be found on github
// https://github.com/ctctsupport/ctct_php_library

include ('wrapper/ConstantContact.php');
	// Connecting to your account
	$ConstantContact = new ConstantContact("basic", "apikey", "username", "password");

	// Get potential contact lists
	$lists = $ConstantContact->getLists();
	// Lists are returned in multidimentional arrays 0 being the list, and 1 being the next50
	
	// Email address here is used for testing purposes
	// NOTE:  This email address will be added to your account if you execute this script,
	//		  you should send this to do-not-mail after your testing
	$emailAddress = "testemailaddress@testemail.com";
	
	// Search for our new Email address
	$search = $ConstantContact->searchContactsByEmail($emailAddress);
	
	// If the search didnt return a contact object
	if($search == false)
	{
		echo "Created new contact";
		// Create a new Contact Object to store data into
		$contactObj = new Contact();
		// Adding multiple lists to this new Contact Object
		$contactObj->lists = array($lists['lists'][0]->id, $lists['lists'][1]->id);
		// Set the email address
		$contactObj->emailAddress = $emailAddress;
		// Create the Contact and DONE
		$Contact = $ConstantContact->addContact($contactObj);
		
		echo $contactObj->emailaddress;
		
	} // Otherwise we update our existing
	else 
	{
		echo $contactObj->emailaddress . " was added to your new list";
		// Gather data from our previous search and store it into a data type
		$contactObj = $ConstantContact->getContactDetails($search[0]);
		// We need to get the old list and add a new list to it as
		// this request requires a PUT and will remove the lists
		// as they are stored in an array
	     array_push($contactObj->lists, $lists['lists'][3]->id );
		
		// Update the contact and DONE
		$UpdateContact = $ConstantContact->updateContact($contactObj);
	}
		

?>

 

Mind you that this does a bit more as it checks to see if it exists, then takes the appropriate actions.

 

Please let me know if you have any other questions.

Ryan Davis
Quality Engineer
RichardN3
Regular Participant

Hi Ryan

 

That's perfect, thanks for the response

Developer Portal

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

Visit Page