Enter the Share Your Success Sweepstakes 2024 For the Chance to Win Great Prizes!

415 Unsupported Media Type when (Java HttpPut)

SOLVED
Go to solution
KristiF2
Campaign Collaborator
0 Votes

I'm having trouble doing an HttpPut in Java. Getting a "415 Unsupported Media Type" error.

 

Here's my put method:

 

    public HttpEntity put(String path, String xml) throws Exception
    {
    	// Use HTTPS
        HttpHost targetHost = new HttpHost("api.constantcontact.com", 443, "https");

        DefaultHttpClient httpclient = new DefaultHttpClient();
        httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(),
    		targetHost.getPort()), new UsernamePasswordCredentials(apikey+"%"+username, password));

        // Use BASIC authentication
        AuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);
        BasicHttpContext localcontext = new BasicHttpContext();
        localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
        
        // Put the request
        HttpPut httpput = new HttpPut("/ws/customers/"+username+path);
        httpput.addHeader("Content-Type", "application/atom+xml");
        StringEntity entity = new StringEntity(xml, "UTF-8");
        entity.setContentType("application/xml");
        httpput.setEntity(entity);
        HttpResponse response = httpclient.execute(targetHost, httpput, localcontext);
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != HttpStatus.SC_OK)
    	{
        	throw new Exception(status.getStatusCode()+" "+status.getReasonPhrase());
    	}
        return response.getEntity();
    }

 Here's the XML entity:

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<entry xmlns="http://www.w3.org/2005/Atom">
    <title type="text"> </title>
    <updated>2008-07-23T14:21:06.407Z</updated>
    <author/><id>data:,none</id>
    <summary type="text">Contact</summary>
    <content type="application/vnd.ctct+xml">
        <Contact xmlns="http://ws.constantcontact.com/ns/1.0/">
            <EmailAddress>nobody@nowhere.com</EmailAddress>
            <OptInSource>ACTION_BY_CUSTOMER</OptInSource>
            <ContactLists>
                <ContactList id="http://api.constantcontact.com/ws/customers/.../lists/0"/>
            </ContactLists>
        </Contact>
    </content>
</entry>

 

 

Thanks.

Tom

 

1 ACCEPTED SOLUTION
DaveBerard
Employee
0 Votes

415 Error indicates that you are making a request that is not allowed on that resource (PUT request not allowed, for example) or that your content type is incorrect.  From your code, it appears that the content type is correct.  My guess would be that the URI you're creating for your request is incorrect.

 

PUT requests are only allows on the actual Contact resource you are trying to update.  For those requests, the format of the URI is:

 

https://api.constantcontact.com/ws/customers/{Username}/contacts/{Contact_ID}

 

Without seeing the actual URI you're posting to, it's hard to tell if this is the actual cause.  415 error does not indicate that anything is wrong with your XML though, 415 errors are thrown before the XML is even parsed (it's a request type validation error, thus we don't even look at the contents of your request).  Can you let us know the actual URI you're makin the PUT to?

Dave Berard
Senior Product Manager, Constant Contact

View solution in original post

11 REPLIES 11
KristiF2
Campaign Collaborator
0 Votes

Looking over this post, I see that the XML looks malformed (no <author> tag). I must have accidentally deleted in in the cut/paste/format effort. This XML is created and rendered using DOM, so the starting <author> tag is surely there.

bsoder
Employee
0 Votes

Hello,

 

I see that you may have resolved this on your own, but I wanted to check in with you to see if you needed any additional help.  If you are still having trouble just let us know!

 

Regards,

Benjamin Soder
NOC Analyst
Constant Contact
KristiF2
Campaign Collaborator
0 Votes

I have not resolved this. I double-checked the XML and it is correct. I removed the XML normalization that resulted in the orphan <author/> tag so that I had "<author> <author/>". Unfortunately, I'm still getting the 415 error. Any help you can offer would be greatly appreciated.

DaveBerard
Employee
0 Votes

415 Error indicates that you are making a request that is not allowed on that resource (PUT request not allowed, for example) or that your content type is incorrect.  From your code, it appears that the content type is correct.  My guess would be that the URI you're creating for your request is incorrect.

 

PUT requests are only allows on the actual Contact resource you are trying to update.  For those requests, the format of the URI is:

 

https://api.constantcontact.com/ws/customers/{Username}/contacts/{Contact_ID}

 

Without seeing the actual URI you're posting to, it's hard to tell if this is the actual cause.  415 error does not indicate that anything is wrong with your XML though, 415 errors are thrown before the XML is even parsed (it's a request type validation error, thus we don't even look at the contents of your request).  Can you let us know the actual URI you're makin the PUT to?

Dave Berard
Senior Product Manager, Constant Contact
KristiF2
Campaign Collaborator
0 Votes

Because this is a new contact (no contact ID yet), I am PUT'ing it to the contacts collection ".../contacts". This is what I interpreted the API to say. After seeing your message, I reread the "Creating a contact" API documentation and it says that I should be POST'ing (not PUT'ing) to the contacts collection. I'll give that a shot, and any other suggestions are welcome.

DaveBerard
Employee
0 Votes

You got it exactly Kristi!  There are some RESTful webservices that interpret PUT as "Update or create if resource doesn't exist".  Our API is more literal in that a POST request is a create (and must be done on the Collection) and a PUT request is an update (and must be done on the individual resource you are updating).  This is because we do create a unique internal ID value for every Contact and thus a PUT request to update or create doesn't make sense in our API.  You should be all set if you change the request type to POST for the creation.

 

Hope this explains it and gets you going!

Dave Berard
Senior Product Manager, Constant Contact
KristiF2
Campaign Collaborator
0 Votes

I'm getting closer. It's now a 400 error (bad request).

 

My post method looks exactly like the PUT method (above). Here's the XML document that I'm POST'ing:

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<entry xmlns="http://www.w3.org/2005/Atom">

<title type="text"> </title>

<updated>2011-04-18T10:54:10.404-0500</updated>

<author> </author>

<id>data:,none</id>

<summary type="text">Contact</summary>

<content type="application/vnd.ctct+xml">

<Contact xmlns="http://ws.constantcontact.com/ns/1.0/ ">

<EmailAddress>nobody@nowhere.com</EmailAddress>

<OptInSource>ACTION_BY_CUSTOMER</OptInSource>

<ContactLists>

<ContactList id="http://api.constantcontact.com/ws/customers/westbanklibrary/lists/0 "/>

</ContactLists>

</Contact>

</content>

</entry>

DaveBerard
Employee
0 Votes

Looks like the problem is your list ID being invalid.  List ID values start at 1 and increment as you create more lists.  0 would always be an invalid List ID number so it should be correctly returning a 400 Bad Request response.

 

For more information on retreiving list IDs, please see this overview: https://community.constantcontact.com/t5/Documentation/Retrieving-a-Contact-List-Collection/ba-p/250...

Dave Berard
Senior Product Manager, Constant Contact
KristiF2
Campaign Collaborator
0 Votes

The problem now is that when I retrieve a contact, it doesn't contain the contact lists. Whether I look it up by "contact-id" or by email address, the result is the same. The following user is (according to the Constant Contact GUI) subscribed to our General Newsletter (/list/2). However, this is the XML that I get back:

 

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?><feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://api.constantcontact.com:443/ws/customers/westbanklibrary/contacts</id>
  <title type="text">Contacts for Customer: westbanklibrary</title>
  <link href="contacts"/>
  <link href="contacts" rel="self"/>
  <author>
    <name>westbanklibrary</name>
  </author>
  <updated>2011-04-18T21:15:39.787Z</updated>
  <link href="/ws/customers/westbanklibrary/contacts" rel="first"/>
  <link href="/ws/customers/westbanklibrary/contacts?email=janet%40property-tax.com" rel="current"/>
  <entry>
    <link href="/ws/customers/westbanklibrary/contacts/3" rel="edit"/>
    <id>http://api.constantcontact.com:443/ws/customers/westbanklibrary/contacts/3</id>
    <title type="text">Contact: janet@property-tax.com</title>
    <updated>2011-04-18T21:15:39.811Z</updated>
    <author>
      <name>Constant Contact</name>
    </author>
    <content type="application/vnd.ctct+xml">
      <Contact xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com:443/ws/customers/westbanklibrary/contacts/3">
        <Status>Active</Status>
        <EmailAddress>janet@property-tax.com</EmailAddress>
        <EmailType>HTML</EmailType>
        <Name/>
        <OptInTime>2011-04-18T18:05:31.102Z</OptInTime>
        <OptInSource>ACTION_BY_CUSTOMER</OptInSource>
        <Confirmed>true</Confirmed>
        <InsertTime>2011-04-18T18:05:31.102Z</InsertTime>
      </Contact>
    </content>
  </entry>
</feed>

 

 

Thanks,

Tom

 

P.S. Kristi is the librarian in charge of the newsletters. I'm the developer.

David_J
Employee
0 Votes

Hi Tom,

 

When Searching For a Contact By Email Address, we will return an overview of any contact(s) that were found in the search. If you are looking to obtain full details, including the contact lists a contact is subscribed to, you would want to then perform a GET on that specific resource to Obtain a Contact's Information. From the XML you pasted above, you would want to follow use 'link' node:

 

<link href="/ws/customers/westbanklibrary/contacts/3" rel="edit"/>

 

and thus perform the GET on https://api.constantcontact.com/ws/customers/westbanklibrary/contacts/3. 

 

This should return the full details for that contact, including their contact lists.

David J

KristiF2
Campaign Collaborator
0 Votes

I think that's exactly what I was missing. Thanks.

 

Tom

Resources
Developer Portal

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

Visit Page

Announcements

API Updates

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

Sign Up