Update Contact - PUT - (415) Unsupported Media Type.

Regular Participant

Update Contact - PUT - (415) Unsupported Media Type.

Ok, I'm racking my brain. I'm trying to POST to a contact. I'm using basically the same code that I found in the various examples (including the C# ones). My XML seems well formed. But I keep getting: (415) Unsupported Media Type. How should I interpret that? My XML is NOT well formed? Or something else?

Here's my code:

For sUserID the string is:
http://api.constantcontact.com/ws/customers//contacts/391
which I GET previously


Uri address = new Uri(CCURL_Contacts);
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Credentials = new NetworkCredential(CCAPIKey + "%" + CCLoginName, CCLoginPassword);
request.Method = "PUT";
request.ContentType = "application/atom+xml";


StringBuilder data = new StringBuilder();
data.Append("<entry xmlns='http://www.w3.org/2005/Atom'>");
data.Append("<id>" + sUserID + "</id>");
data.Append("<title type='text'>Contact: " + sUEmail + "</title>");
data.Append("<updated>2008-04-25T19:29:06.096Z</updated>");
data.Append("<author>");
data.Append("<name>Constant Contact</name>");
data.Append("</author>");
data.Append("<content type='application/vnd.ctct+xml'>");
data.Append("<Contact xmlns='http://ws.constantcontact.com/ns/1.0/' id='" + sUserID + "'>");
data.Append("<EmailAddress>" + sUEmail + "</EmailAddress>");
data.Append("<FirstName>Joe</FirstName>");
data.Append("<LastName>Smith</LastName>");
data.Append("<OptInSource>ACTION_BY_CUSTOMER</OptInSource>");
data.Append("<ContactLists>");
data.Append("<ContactList id='http://api.constantcontact.com/ws/customers/" + CCAccountName + "/lists/1'/>");
data.Append("<ContactList id='http://api.constantcontact.com/ws/customers/" + CCAccountName + "/lists/2'/>");
data.Append("</ContactLists>");
data.Append("</Contact>");
data.Append("</content>");
data.Append("</entry>");



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



// Set the content length in the request headers
request.ContentLength = byteData.Length;



try
{


// Write data
Stream postStream = request.GetRequestStream();
postStream.Write(byteData, 0, byteData.Length);
postStream.Close();


string results = String.Empty;


// Get response
HttpWebResponse response = request.GetResponse() as HttpWebResponse;


// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
results = Response.StatusCode + " " + Response.StatusDescription + " " + reader.ReadToEnd();
reader.Close();


return results;


}
catch (WebException e)
{
// Get the web exception type and response code
return "WebException: " + e.Status + " With response: " + e.Message;
}
catch (Exception e)
{
// Get the exception type
return "Exception:" + e.Message;
}
25 REPLIES 25
Regular Participant

So now I've tried just PUTing back the entry I GET without changes, using two different queries:

URI= http://api.constantcontact.com/ws/customers//contacts/391"

URI= http://api.constantcontact.com/ws/customers//contacts?email=xxxxx@xxxxx.com"

(I've hidden my account & the email addr.)

Still error 415 Unsupported Media Type.

Where's the error ???? Any help is appreciated. I've been looking through all the code on the site and I'm running out of ideas.
Regular Participant

the URI I am PUT'ing to is:

http://api.constantcontact.com/ws/customers//contacts
Frequent Participant

Your PUT should be to the specific URI associated with the contact you are updating.

So your URI should be:
URI= http://api.constantcontact.com/ws/customers//contacts/391

I would expect a 415 response if you are PUTting to another URI . Do you get a 415 when you use this specific contact URI?

What happens if you simply "PUT" back the same entry to received in the GET?
Tom M
Group Product Manager – Content Editing
Constant Contact
Regular Participant

Tom, thanks for your prompt response. I was just not appending the contact id # to the contacts URI. Doh! I will try it later when I get to my development machine. Thanks again!
Regular Participant

Tom, I fixed the URI and hurray it works!

But now I keep getting error 400 Bad Request.
Again, I'm trying out all examples, also just PUT'ing back what I GET for the user entry. I imagine it will be something simple that I'm overlooking.

Thanks in advance for helping out.

Some attempts:


data.Append("<entry>");
data.Append("<link href='/ws/customers/MYACCOUNT/contacts/391' rel='edit' />");
data.Append("<id>http://api.constantcontact.com/ws/customers/MYACCOUNT/contacts/391</id>");
data.Append("<title type='text'>Contact: XXXXXX@gmail.com</title>");
data.Append("<updated>2008-11-09T14:48:43.622Z</updated>");
data.Append("<author>");
data.Append("<name>Constant Contact</name>");
data.Append("</author>");
data.Append("<content type='application/vnd.ctct+xml'>");
data.Append("<Contact xmlns='http://ws.constantcontact.com/ns/1.0/' id='http://api.constantcontact.com/ws/customers/MYACCOUNT/contacts/391'>");
data.Append("<Status>Active</Status>");
data.Append("<EmailAddress>XXXXX@gmail.com</EmailAddress>");
data.Append("<EmailType>HTML</EmailType>");
data.Append("<Name></Name>");
data.Append("<OptInTime>2008-04-05T22:16:58.575Z</OptInTime>");
data.Append("<OptInSource>ACTION_BY_CONTACT</OptInSource>");
data.Append("<Confirmed>true</Confirmed>");
data.Append("<InsertTime>2008-03-30T16:48:24.441Z</InsertTime>");
data.Append("</Contact>");
data.Append("</content>");
data.Append("</entry>");


Or:
(this one I got from GET'ing)

data.Append("<entry xmlns='http:www.w3.org/2005/Atom'>");
data.Append("<link href='/ws/customers/MYACCOUNT/contacts/391' rel='edit' />");
data.Append("<id>http:api.constantcontact.com/ws/customers/MYACCOUNT/contacts/391</id>");
data.Append("<title type='text'>Contact: XXXXX@gmail.com</title>");
data.Append("<updated>2008-04-05T22:18:01.554Z</updated>");
data.Append("<author>");
data.Append("<name>Constant Contact</name>");
data.Append("</author>");
data.Append("<content type='application/vnd.ctct+xml'>");
data.Append("<Contact xmlns='http:ws.constantcontact.com/ns/1.0/' id='http:api.constantcontact.com/ws/customers/MYACCOUNT/contacts/391'>");
data.Append("<Status>Active</Status>");
data.Append("<EmailAddress>XXXXXX@gmail.com</EmailAddress>");
data.Append("<EmailType>HTML</EmailType>");
data.Append("<Name>Sample</Name>");
data.Append("<FirstName>Sample</FirstName>");
data.Append("<MiddleName></MiddleName>");
data.Append("<LastName>Sample</LastName>");
data.Append("<JobTitle></JobTitle>");
data.Append("<CompanyName></CompanyName>");
data.Append("<HomePhone></HomePhone>");
data.Append("<WorkPhone>123.456.7890</WorkPhone>");
data.Append("<Addr1>240 Main</Addr1>");
data.Append("<Addr2></Addr2>");
data.Append("<Addr3></Addr3>");
data.Append("<City></City>");
data.Append("<StateCode></StateCode>");
data.Append("<StateName></StateName>");
data.Append("<CountryCode></CountryCode>");
data.Append("<CountryName></CountryName>");
data.Append("<PostalCode></PostalCode>");
data.Append("<SubPostalCode></SubPostalCode>");
data.Append("<Note></Note>");
data.Append("<CustomField1></CustomField1>");
data.Append("<CustomField2></CustomField2>");
data.Append("<CustomField3></CustomField3>");
data.Append("<CustomField4></CustomField4>");
data.Append("<CustomField5></CustomField5>");
data.Append("<CustomField6></CustomField6>");
data.Append("<CustomField7></CustomField7>");
data.Append("<CustomField8></CustomField8>");
data.Append("<CustomField9></CustomField9>");
data.Append("<CustomField10></CustomField10>");
data.Append("<CustomField11></CustomField11>");
data.Append("<CustomField12></CustomField12>");
data.Append("<CustomField13></CustomField13>");
data.Append("<CustomField14></CustomField14>");
data.Append("<CustomField15></CustomField15>");
data.Append("<ContactLists>");
data.Append("<ContactList id='http:api.constantcontact.com/ws/customers/MYACCOUNT/lists/1'>");
data.Append("<link xmlns='http:www.w3.org/2005/Atom' href='http:api.constantcontact.com/ws/customers/MYACCOUNT/lists/1' rel='self' />");
data.Append("<OptInSource>ACTION_BY_CONTACT</OptInSource>");
data.Append("<OptInTime>2008-04-05T22:18:01.557Z</OptInTime>");
data.Append("</ContactList>");
data.Append("</ContactLists>");
data.Append("<Confirmed>true</Confirmed>");
data.Append("<InsertTime>2008-03-30T16:48:24.441Z</InsertTime>");
data.Append("<LastUpdateTime>2008-04-05T22:18:01.554Z</LastUpdateTime>");
data.Append("</Contact>");
data.Append("</content>");
data.Append("<source>");
data.Append("<id>http:api.constantcontact.com/ws/customers/MYACCOUNT/contacts</id>");
data.Append("<title type='text'>Contacts for Customer: MYACCOUNT</title>");
data.Append("<link href='http:api.constantcontact.com/ws/customers/MYACCOUNT/contacts' />");
data.Append("<link href='http:api.constantcontact.com/ws/customers/MYACCOUNT/contacts' rel='self' />");
data.Append("<author>");
data.Append("<name>MYACCOUNT</name>");
data.Append("</author>");
data.Append("<updated>2008-11-09T15:06:02.773Z</updated>");
data.Append("</source>");
data.Append("</entry>");

Frequent Participant

The first entry above does not seem to have a ContactLists tag, which I believe is required.

For the second entry, is your Contact List: "lists/1" on your sign--up form? If it is not, you must indicate that the Contact is being Added by Customer, not Added by Contact. You do this by indicating the OptInSource **Outside** of the <ContactLists> section. So the middle of your XML would look something like this:

<EmailAddress>u86597@example.com</EmailAddress>
<OptInSource>ACTION_BY_CUSTOMER</OptInSource>
<ContactLists>
<ContactList id="http://api.constantcontact.com/ws/customers/joesflowers/lists/1"></ContactList>

I was able to reproduce your 400 error when I did not include this tag.
Tom M
Group Product Manager – Content Editing
Constant Contact
Regular Participant

Tom, I finally got it to work. I went back to the the documentation on:
http://developer.constantcontact.com/doc/contactsCollection

Thanks for your help!
Regular Participant

daedalux,
I'm starting out trying to program something similar. With the code that you poste here, how did you run that? Is it in a webpage? an xml File? I just can't get the idea into my head.
Thanks
Moderator

Hi jmestep,

The way you use .NET code varies greatly depending on how you create your application. There are a few ways to do this in as Windows application, from an old style command line application to a windows stand alone application. The newest method of using this is to create asp webpages that use VB or C# code as the scripting language for the website. There are a lot of examples on how to do this in the MSDN help files. Once you understand how to create a website or form in asp .NET, I think it will make more sense to you how to use this code on your website.

Hope this helps point you in the right direction!
Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

I have seen that u succeeded to update contacts in constant contacts from c#.Am also trying to do the same.and getting the same error UnSupported Mediatype errorcode 415.please help me to solve this issue.

Regular Participant

Please help me to solve the issues am facing with constant contact integration with ASP.Net3.5.Am trying to Update and Delete contacts from constant contact.But unfortunately its not working.But i secceeded to run the programs for adding,listing and search contacts.Please help me to findout a solution

Moderator

We offer a sample ASP.NET sign up form complete with a REST library you can use in other applications.  Please take a look at the sample code here.  This should help you get started, please let me know if there are any other questions once you take a look at that sample code.

Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

   string username = "myusername";

        string contact = "  https://api.constantcontact.com/ws/customers/{myname}/contacts?email=" + txt_email_search.Text.Trim().ToLower();

        CredentialCache newCredentialCache = new CredentialCache();

        newCredentialCache.Add(new Uri("https://api.constantcontact.com/ws/customers/{myname}"), "Basic", new NetworkCredential(username, "pwd"));

        WebRequest webRequest = WebRequest.Create(contact);

        webRequest.Credentials = newCredentialCache;

        //try

        //{



            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

            StreamReader streamReader = new StreamReader(webResponse.GetResponseStream());

            string XMLResponse =  streamReader.ReadToEnd();

            webResponse.Close();

            streamReader.Close();





            XMLResponse = XMLResponse.Replace("HTML", "TEXT");

            string contacts = "https://api.constantcontact.com/ws/customers/{myname}/contacts/" + HiddenField1.Value.ToString();

            HttpWebRequest httpwebRequest = (HttpWebRequest)WebRequest.Create(contacts);

            httpwebRequest.Method = "PUT";

            httpwebRequest.ContentType = "application/atom+xml";

            httpwebRequest.Credentials = newCredentialCache;

            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(XMLResponse);

            httpwebRequest.ContentLength = byteArray.Length;

            Stream strStream = httpwebRequest.GetRequestStream();

            strStream.Write(byteArray, 0, byteArray.Length);

            strStream.Close();

            HttpWebResponse webResponse2 = (HttpWebResponse)httpwebRequest.GetResponse();

            StreamReader streamReader2 = new StreamReader(webResponse2.GetResponseStream());

            Response.Write(streamReader2.ReadToEnd());

            webResponse2.Close();

            streamReader2.Close();


 


 


this is the code i tried for updation.But it returns an error coded 400 on webResponse2.I don't know why?Please help


*txt_search_email is a textbox having the id email to search and update


*HiddenFiled1 has the contactid of the particularemailid




 

 




Moderator

There is a problem with what you are looking to do.  The response XML from a contact search, such as you are doing, is a minimal XML set to identify who you are looking for.  While the search you are using will only return a single contact, the search feature can be also used to search by update time, for example, which will return potentially hundreds.


 


This XML is not formed correctly for an update to the Contacts collection.  If you look at what was returned, you have the Contact ID in there for pulling out the full XML for that contact.  That returned XML can be used for updates to the Contacts collection.   For more information on these XML documents, please see our Contacts collection documentation.

Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

Then what should i supposed to do with the search for getting a full formatted xml ?am in hurry

Regular Participant

i got almost all methods like add/remove contacts.Add a new list,list all contacts,list contacts by list.


now am struggling to delete a list from my asp.net website.


please help me..


 

Regular Participant

I GOT ALMOST ALL I NEED.. THANKS FOR YOUR HELP AND SUPPORT...


NOW WANTS TO GO ON WITH EMAILCAMPAIGN ..EXPECTING YOUR HELP...


 


JEEVAN BABY

Moderator

Glad to hear things are going well.  To delete any resources in REST API, you just need to run a DELETE HTTP request on the resource URI.  For a list, that URI would look like this:


 


https://api.constantcontact.com/ws/customers/{username}/lists/{listID}


 


Set the request type to DELETE and this will delete the list you specified with the listID.  Hope this helps!

Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

thanks for your support dave.I succeeded in my attempt and integrate constantcontact with my asp.net application using api and by using some other methods suggested by some of the experts.


Thanks alot


 


Jeevan Baby

Occasional Participant

From where we get Contact id( for example 391)...It's very urgent?


Suppose i have created three list in my account and now i want to add different e-mails on all the three list list then how do i know the {contact id}?


URI= http://api.constantcontact.com/ws/customers//contacts/391


Please help ASAP! 


Shalin

Developer Portal

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

Visit Page