Unsupported Media Type error when posting to Web API
64,320
Solution 1
You should set the media type when creating StringContent
new StringContent(json, Encoding.UTF32, "application/json");
Solution 2
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;
Solution 3
To fix the unsupported media type I had to use HttpRequestMessage and add header to accept json with MediaTypeWithQualityHeaderValue like bellow.
var httpRequestMessage = new HttpRequestMessage
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var httpResponse = await _client.PostAsync("/contacts", httpRequestMessage.Content);
![Billson](https://lh6.googleusercontent.com/-GrEg_u6PlWY/AAAAAAAAAAI/AAAAAAAAABQ/6f145kY5DwE/photo.jpg?sz=256)
Author by
Billson
Updated on July 09, 2022Comments
-
Billson almost 2 years
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; } }
-
MichaelD about 8 yearsSomehow it doesn't work with Encoding.UTF32 . Encoding.UTF8 does work. Any explanation?
-
MichaelD about 8 yearsNo error, the values aren't parsed into the model(they stay null)
-
Pedro Drewanz about 8 yearsI would need more details, about your server code. It is probably some server configuration.
-
dudeNumber4 about 5 yearsIf you're using C# / HttpClient, this is the issue (completely unrelated to other issues mentioned here).
-
djack109 about 4 yearsthis doesn't work for me, anyone got a more complete example?
-
Muhammad Usama Alam about 3 yearsin my case UTF8 Works new StringContent(JsonString, Encoding.UTF8, "application/json");