"415 Unsupported Media Type" For Content-Type "application/csp-report" In ASP.NET Core


Answer :

The following example shows how to add support to the SystemTextJsonInputFormatter for handling additional media-types:

services.AddControllers(options => {     var jsonInputFormatter = options.InputFormatters         .OfType<SystemTextJsonInputFormatter>()         .Single();      jsonInputFormatter.SupportedMediaTypes.Add("application/csp-report"); }); 

This is a two-step process:

  1. Interrogate the configured list of input-formatters to find the SystemTextJsonInputFormatter.
  2. Add application/csp-report to its existing list of supported media-types (application/json, text/json, and application/*+json).

If you're using Json.NET instead of System.Text.Json, the approach is similar:

services.AddControllers(options => {     var jsonInputFormatter = options.InputFormatters         .OfType<NewtonsoftJsonInputFormatter>()         .First();      jsonInputFormatter.SupportedMediaTypes.Add("application/csp-report"); }) 

There are two small differences:

  1. The type is NewtonsoftJsonInputFormatter instead of SystemTextJsonInputFormatter.
  2. There are two instances of this type in the collection, so we target the first (see this answer for the specifics).

See Input Formatters in the ASP.NET Core docs to learn more about those.


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable