Reputation: 1126
Hello I have problem with configuring/testing Swashbuckle/Swagger 5.6.0 with .NET Framework 4.8 (ASP.NET [not ASP.NET Core!])
Testing GET methods from WebUI Swagger throws an error.
Doing the same with postman is:
Content-Type: "application/json" - OK
Content-Type: "application/ocet-stream" - same error
Response body error:
Message: "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.",
ExceptionMessage: "No MediaTypeFormatter is available to read an object of type 'SomeParams' from content with media type 'application/octet-stream'.",
ExceptionType: "System.Net.Http.UnsupportedMediaTypeException",
StackTrace: " w System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n w System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
Looks like there's problem with missing Request Content-Type header and I am unable to set it up properly inside SwaggerWebUI.
There's no Produces / Consumes attribute in ASP.NET Swashbuckle so I added them manually (customattribute + customfilters + registration in SwaggerConfig.cs) following instructions from: https://blog.kloud.com.au/2017/08/04/swashbuckle-pro-tips-for-aspnet-web-api-part-1/ But its not working and Parameter content type combobox doesn't appear.
I also have tried editing WebApiConfig.cs with/without Global/normal formatters adding/clearing SupportedMediaTTypes etc.
//config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
var mediaType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var formatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
formatter.SupportedMediaTypes.Clear();
formatter.SupportedMediaTypes.Add(mediaType);
config.Formatters.Clear();
config.Formatters.Add(formatter);
var jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
jsonFormatter.SupportedMediaTypes.Clear();
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
GlobalConfiguration.Configuration.Formatters.Add(jsonFormatter);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
Sample Controller code:
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace MainClass
{
public class xxx_V1Controller : ApiController
{
[HttpGet]
[SwaggerConsumes("application/json")]
[SwaggerProduces("application/json")]
[Route("api/xxx_V1/GetAll/", Name = "xxx_V1_GetAll")]
public Rxxx_V1 GetAll(SomeParamsXyz)
{
return AuthorizationController.MainClass.yyy_GetAll_V1(SomeParams xyz);
}
[HttpGet]
[Route("api/xxx_V1/GetAll2/", Name = "xxx_V1_GetAll2")]
public Rxxx_V1 GetAll2(SomeParams xyz)
{
return AuthorizationController.MainClass.yyy_GetAll2_V1(SomeParams xyz);
}
}
}
SomeParams Class
public class SomeParams : IXyz, IFilters
{
public string XXX { get; set; }
public bool GetCountOnly { get; set; }
public bool YYY{ get; set; }
public LoadOptions Options { get; set; }
public List<Filter> Filters { get; set; }
}
http://localhost:20220/swagger/docs/v1 fragment:
"/api/xxx_V1/GetAll": {
"get": {
"tags": [
"xxx_V1"
],
"operationId": "xxx_V1_GetAll",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "options.xxx",
"in": "query",
"required": false,
"type": "string"
},
{
"name": "options.getCountOnly",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.YYY",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.options.getCountOnly",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.options.YYY",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.filters",
"in": "query",
"required": false,
"type": "array",
"items": {},
"collectionFormat": "multi"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/xxxx.Objects.Rxxx_V1"
}
}
}
}
},
Custom filters Consumes / Produces propably work beacuse all other Get Methods without those attribute has json with application/xml too.
"consumes": [
"application/json",
"application/xml"
],
"produces": [
"application/json",
"application/xml"
],
Upvotes: 2
Views: 3352
Reputation: 65
I found a site that stated the Swashbuckler team decided to not support GET calls with a Body. I tried all the above solutions, but none worked for me. The solution for me was to change the action to a POST. Sorry I don't have a link to the discussion. But here is GitHub reporting it as Bug, but it was closed. https://github.com/swagger-api/swagger-ui/issues/5388
Upvotes: 1