Search code examples
c#asp.net-web-apijson-ldmediatypeformatter

Unsupported media type formatter when sending JSON-LD


I am sending JSON LD data using HttpClient in C# to a webapi end point. I get the error unsupported media type formatter. The data I am sending is JSONLD as a string:

string data = @"{
                      ""@type"": ""vcx:blah"",
                      ""vcx:key"": ""blah"",
                      ""vcx:value"": ""blah""
                 } ";
httpClient.PostAsync(uri, new StringContent(data));

On the api side, is web api. In the Global.asax.cs I have the following:

GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new 
JsonMediaTypeFormatter());

Even if i remove the clearing of the formatters I get the same error on the calling application.

Anyone know the solution?


Solution

  • See docs

    You are using the first constructor of StringContent, it means that the media type for the StringContent created defaults to text/plain

    If you need jsono then specify media type:

    httpClient.PostAsync(uri, new StringContent(data, Encoding.UTF8, "application/json"));