Create Contact API Java Not working

0 Votes

When I use the provided Java Code I either get an invalid media type or invalid JSON as an issue. Any help regarding this is appreciated.

0 Votes

Hello SharmilaG4,


Thank you for reaching out to Constant Contact API Developer Support. My team is here to assist outside software developers with questions about building into Constant Contact's API.


Based on the key generated within the account that you are posting from, I was able to identify three error responses within our logs that you seem to be running into when making POST calls to the /contacts endpoint: 400, 415, & 409. You may have already resolved one or more of these issues, but I’ll still expand on all three for visibility in the forum:


V3 API - Response Codes


400: Bad Request

Description: Your request includes malformed JSON or does not conform to the expected schema


It looks like in your JSON request body, here was an additional comma at the end of your data that needed to be removed. After "United States", there are no other values so programmatically the comma is what causes the error.:

"country": "United States",



Additionally, while it won’t cause an error per se, it looks like the list value was missing in the request body, so the added contact would not exist on any list, making it so that you cannot send to them.


415: Unsupported Media Type

Description: The media format type that you are specifying for a given resource and method is not supported. For example, attempting to send a request with text/plain as the content-type in the header with a method that requires application/JSON produces a 415 error response code.


Based on our logs for calls made by your key that returned a 415 response, it looks like your “content-type” header was set to “text/plain”. This is easy to miss, you'll just need to change it to “application/JSON”.


409: Conflict

Description: You are attempting to create (POST) or update (PUT) a resource with a name that already exists for a resource within the account. For example, attempting to create an email address or email campaign name that already exists produces a 409 error response code.


On your more recent calls, I’m seeing some 409 responses:


Once a contact (email address) has been added into the system, even if it is deleted, the contact entry and details still remain on the back end of the system for reporting and compliance purposes. When using the API, there are a couple of different endpoint options to bypass the 409 response. Typically this error occurs when making a POST call to the /contacts endpoint, which can only add new contacts whose email addresses have not previously been added to the account.


For example, if a contact unsubscribes from your organization’s mailing list, and you were to delete and then re-add that contact, they would be re-added as unsubscribed rather than as a new contact. This allows us to be compliant with the CAN-SPAM Act, as well as other applicable laws and regulations. 


CAN-SPAM Act and how it affects your campaigns


Frequently Asked Questions for Contacts


Essentially, deleting a contact never really deletes them. It just strips their list memberships, moves them into a “deleted” status, and hides them from being visible in the account.


The three methods listed below each offer a different way to add contacts via the API without encountering a 409 response, depending on how your integration is configured:


[[Option 1 (uses one API call, preferred endpoint for sign-up forms):]]


Use the sign-up form endpoint, to add a new contact to an account or update an existing contact based on their email address. Only use this method when a contact gives you their explicit permission to send them emails.


Create or Update a Contact Using a Single Method (less recommended)


[[Option 2 (uses two API calls, allows you to overwrite/remove data):]]


Use this flow to add/update your contacts either via a sign-up form or other integration type which incorporates three contact endpoints:


Step 1: Check to see if the contact exists in the account using the email query parameter to search for a contact using a specific email address.


GET Contacts Collection!/Contacts/getContacts


Step 2: If the contact comes back as non-existent, use POST to create the contact:


POST (create) a Contact!/Contacts/createContact


Step 3: If the contact comes back as already existing in the account. You need to append the returned data with the desired list memberships and send that back with PUT to update the contact:


**When Updating a contact, we need to append the new information to the old information and send all of it back in the request, because when you update a contact using a PUT, all properties are updated, overwriting all existing property values. Any properties left blank or not included in the PUT will be overwritten with a null value.


PUT (update) a Contact!/Contacts/putContact


[[Option 3 (uses one API call, but can use two to verify activity completed successfully, good for syncing contacts between two systems):]]


You can use our bulk activity endpoints to import new contacts and update existing contacts when syncing local data with Constant Contact. These endpoints use the email addresses you provide in the import to determine if each contact is new or not. When you import an existing contact, these endpoints only update the contact properties you include in the import request.


Import Contacts Using Bulk Activity Endpoints


Please have a look and let us know if you have any other questions!


Courtney E.
Tier II API Support Engineer

Did I answer your question?
If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.
Developer Portal

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

Visit Page


API Updates

Join our list to be notified of new features and updates to our V3 API.

Sign Up