(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);
            newStream.Close();

            WebResponse response = myRequest.GetResponse();
perlwhiteAsked:
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:
http://brockallen.com/2012/05/14/http-status-codes-for-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:
http://social.msdn.microsoft.com/Forums/is/wcf/thread/bcadd339-662d-4751-b96a-ac09fd297d00

You should check that your configuration is correct -- this is a nice explanation of an error in configuring the server:
http://blogs.msdn.com/b/endpoint/archive/2010/11/01/wcf-webhttp-service-returns-http-415-unsupported-media-type.aspx

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

HTTP POST request issue

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

http://comments.gmane.org/gmane.comp.db.couchdb.user/9198
0
 
perlwhiteAuthor Commented:
Thank you both, however either of the solutions did not work. I get the same runtime error.
0
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.