(415) Unsupported Media Type

I get the (415) Unsupported Media Type error when I use POST in REST. I am using C# and Visual Web developer 2010 Express. I do not control the service. Here is the partial code:

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

myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = data.Length;

            Stream newStream = myRequest.GetRequestStream();
            newStream.Write(data, 0, data.Length);

            WebResponse response = myRequest.GetResponse();
Who is Participating?
mrcoffee365Connect With a Mentor Commented:
Several things can cause the 415 HTTP response code.  See this nice summary for descriptions of all the HTTP response codes you are likely to hit using REST:

Probably the content type you're requesting is not correct for what is being returned.  To debug, you should turn on tracing for your requests to see what is in the header you are sending.  See this discussion from msdn about the possible causes of the 415 HTTP Response Code:

You should check that your configuration is correct -- this is a nice explanation of an error in configuring the server:

And finally, this is a nice verbose discussion of 415 that you might find educational:
Alan WarrenConnect With a Mentor Applications DeveloperCommented:
You may need to use myRequest.ContentType = "multipart/form-data; boundary=";

HTTP POST request issue

Easwaran ParamasivamConnect With a Mentor Commented:
Content-Type: application/json could be used. Please check it out:

perlwhiteAuthor Commented:
Thank you both, however either of the solutions did not work. I get the same runtime error.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.