The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

POST method 415 unsupported media type error

Highlighted
New Member

POST method 415 unsupported media type error

HI,


I am trying to save a single contact to my account using the post method. I can post the contact successfully using the bulk post method. But when I try to post a single contact using the following code, getting a 415 unsupported media type error. I have used the same sample code provided in your forum. Idon't have much hair ramining to pull out :) . Could some help me to get out of this error ?


public Boolean CreateNewContact(String strEmail, String strFirstName, String strLastName, String strAge)

    {

        // get the Atom entry for specified Contact

        StringBuilder data = CreateAtomEntry(strEmail, strFirstName, strLastName, strAge);

       

        System.IO.Stream stream = System.IO.Stream.Null;

        try

        {

            // post the Atom entry at specified Uri and save the response stream

            stream = GetResponseStream(new Uri(@"https://api.constantcontact.com/ws/customers/" + cc_user + "/contacts/12111"), data.ToString());



            // return newly created Contact

            return GetContactDetails(stream);

        }

        catch (Exception e)

        {

            return false;

        }

        finally

        {

            // close the response stream

            stream.Close();

        }

    }



    private StringBuilder CreateAtomEntry(String strEmail, String strFirstName, String strLastName, String strAge)

    {

        StringBuilder data = new StringBuilder();

        data.AppendFormat("<entry xmlns='{0}'>", atomNamespace);

        data.Append("<title type='text'> None </title>");

        data.Append("<updated>2008-07-23T14:21:06.407Z</updated>");

        data.Append("<author> None </author>");

        data.Append("<id>data:,none</id>");

        data.Append("<summary type='text'>Contact</summary>");

        data.Append("<content type='application/vnd.ctct+xml'>");

       

        data.AppendFormat("<Contact xmlns='{0}'>", constantNamespace);

        data.AppendFormat("<EmailAddress>{0}</EmailAddress>", strEmail);

        data.AppendFormat("<FirstName>{0}</FirstName>", strFirstName);

        data.AppendFormat("<LastName>{0}</LastName>", strLastName);

        data.AppendFormat("<CustomField1>{0}</CustomField1>", strAge);

       

        data.Append("<OptInSource>ACTION_BY_CONTACT</OptInSource>");

       

        data.Append("<ContactLists>");

        data.Append("<ContactList id='http://api.constantcontact.com/ws/customers/" + cc_user + "/lists/12111'/>");

        data.Append("</ContactLists>");

       

        data.Append("</Contact>");

        data.Append("</content>");

        data.Append("</entry>");



        return data;

    }

   

    private System.IO.Stream GetResponseStream(Uri address, string data)

    {

        //byte[] dataByte = Encoding.ASCII.GetBytes(data);

        byte[] dataByte = Encoding.UTF8.GetBytes(data);

       

        System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(address);

        request.Credentials = new System.Net.NetworkCredential((cc_api_key + "%" + cc_user), cc_pswd);

       

        request.Method = "POST";

        // set the content type of the data being posted

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

        // request MUST include a WWW-Authenticate

        request.PreAuthenticate = true;

       

        // set the content length of the data being posted

        request.ContentLength = dataByte.Length;

       

        // write data

        System.IO.Stream stream = request.GetRequestStream();

        stream.Write(dataByte, 0, dataByte.Length);

       

        System.IO.Stream responseStream = System.IO.Stream.Null;

        try

        {

            // get the response Stream

            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();

           

            if (response.StatusCode == System.Net.HttpStatusCode.NoContent)

            {

                // server don't send any response to us

                return System.IO.Stream.Null;

            }

           

            // get the response Stream

            responseStream = response.GetResponseStream();

           

            // read the response stream and save it into a memory stream

            System.IO.MemoryStream memoryStream = ReadResponseStream(responseStream);

           

            return memoryStream;

        }

        finally

        {

            // close the Stream object

            stream.Close();

           

            // close response stream; it also closes the web response

            responseStream.Close();

        }

    }


 


 


Thanks in advance


Renjith~

2 REPLIES 2
Highlighted
Occasional Advisor

Adding a new contact

Hello Renjith,


Are you currently trying to add a new contact, or update an existing contact?


 


If you are adding a new contact, you need to perform a POST to the following URI:


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

 


If you are updating an existing contact, you need to perform a PUT to the following URI:


https://api.constantcontact.com/ws/customers/{username}/contacts/{contact-id}

 


More information can be found HERE.

Andrew T

Support Engineer, Constant Contact

Highlighted
New Member

Adding a new contact

Hi Andrew,


Thank you very much for the help! I have removed the {contact-id}from the URI and it worked...


Renjith~

Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured