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

Highlighted
Occasional 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
Highlighted
Occasional Participant

Next C# code test: PUT back what I GET - still 415

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.
Highlighted
Occasional Participant

another detail

the URI I am PUT'ing to is:

http://api.constantcontact.com/ws/customers//contacts
Highlighted
Developer

re: Next C# Code-> You should PUT to Specific Contact URI

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
Highlighted
Occasional Participant

Thanks

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!
Highlighted
Occasional Participant

400 Bad Request

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>");

Highlighted
Developer

re: 400 BAD REQUEST

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
Highlighted
Occasional Participant

Fixed

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!
Highlighted
Occasional Participant

daedalux, I'm starting out

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
Highlighted
Moderator

RE: daedalux, I'm starting out

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
Developer Portal

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

Visit Page

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured