Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsupported Media Type error when posting to Web API

Making a windows phone application and although I may easily pull from my Web Api I am having trouble posting to it. Whenever posting to the api I get the "Unsupported Media Type" error message and I'm not sure as to why it is happening considering the class I using as the base for my JSON post is the same as the one used in the api.

PostQuote (Post Method)

private async void PostQuote(object sender, RoutedEventArgs e)         {             Quotes postquote = new Quotes(){                 QuoteId = currentcount,                 QuoteText = Quote_Text.Text,                 QuoteAuthor = Quote_Author.Text,                 TopicId = 1019             };             string json = JsonConvert.SerializeObject(postquote);             if (Quote_Text.Text != "" && Quote_Author.Text != ""){                  using (HttpClient hc = new HttpClient())                 {                     hc.BaseAddress = new Uri("http://rippahquotes.azurewebsites.net/api/QuotesApi");                     hc.DefaultRequestHeaders.Accept.Clear();                     hc.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));                     HttpResponseMessage response = await hc.PostAsync(hc.BaseAddress, new StringContent(json));                     if (response.IsSuccessStatusCode)                     {                         Frame.Navigate(typeof(MainPage));                     }                     else                     {                         Quote_Text.Text = response.StatusCode.ToString();                         //Returning Unsupported Media Type//                     }                 }             }         } 

Quotes and Topic (Model)

public class Quotes     {         public int QuoteId { get; set; }         public int TopicId { get; set; }         public string QuoteText { get; set; }         public string QuoteAuthor { get; set; }         public Topic Topic { get; set; }         public string QuoteEffect { get; set; }     }     //Topic Model//     public class Topic     {         public int TopicId { get; set; }         public string TopicName { get; set; }         public string TopicDescription { get; set; }         public int TopicAmount { get; set; }     } 
like image 823
Billson Avatar asked Jul 20 '15 21:07

Billson


People also ask

How do I fix unsupported media type error?

Fixing 415 Unsupported Media Type errorsEnsure that you are sending the proper Content-Type header value. Verify that your server is able to process the value defined in the Content-Type header. Check the Accept header to verify what the server is actually willing to process.

What is media type in Web API?

The media type determines how Web API serializes and deserializes the HTTP message body. Web API has built-in support for XML, JSON, BSON, and form-urlencoded data, and you can support additional media types by writing a media formatter. To create a media formatter, derive from one of these classes: MediaTypeFormatter.

What is unsupported media type in Postman?

Http 415 Media Unsupported is responded back only when the content type header you are providing is not supported by the application. With POSTMAN, the Content-type header you are sending is Content type 'multipart/form-data not application/json .

How do I fix status 415?

The 415 Unsupported Media Type error code usually indicates that the request entity has a media type that the server or resource does not support. The best way to fix the error is to make sure that you are sending the right Content-Type header value.


2 Answers

You should set the media type when creating StringContent

new StringContent(json, Encoding.UTF32, "application/json"); 
like image 78
Pedro Drewanz Avatar answered Oct 09 '22 04:10

Pedro Drewanz


I found this question while working on a quick and dirty reverse proxy. I needed form data and not JSON.

This did the trick for me.

string formData = "Data=SomeQueryString&Foo=Bar"; var result = webClient.PostAsync("http://XXX/api/XXX",          new StringContent(formData, Encoding.UTF8, "application/x-www-form-urlencoded")).Result; 
like image 37
Robert Stokes Avatar answered Oct 09 '22 04:10

Robert Stokes