0

I have an ASP.Net Core 2.0 Web API project that utilizes NSwag to get Swagger functionality. The problem is that when I use the SwaggerUI to test the API, the FromBody parameter is not working. However when I use Postman with the same body and application/json selected for the header (as in SwaggerUi), everything works.
In the debugger I noticed that the requests from Postman have a trailing slash, while the requests from SwaggerUi dont.

Here is the action from the controller:

[HttpPost("")]
[SwaggerResponse(HttpStatusCode.Created, typeof(BlobItem))]
public async Task<IActionResult> Create([FromBody] BlobItem item)
{
    if (item == null)
    {
        return this.BadRequest("the item is null");
    }

    var blobItem = await BlobHelpers.PostBlobItemAync(this.container, item);

    return this.CreatedAtRoute("GetBlobInfo", new { blobName = item.Name }, blobItem);
}

Postman -> item is filled
SwaggerUi -> item is null


I tried adding a trailing slash to the default route in Startup

app.UseSwaggerUi(typeof(Startup).GetTypeInfo().Assembly, new SwaggerUiSettings()
{
    DefaultUrlTemplate = "{controller}/{action}/{id}/",
    DefaultPropertyNameHandling = NJsonSchema.PropertyNameHandling.CamelCase,
});

without success and also

services.AddRouting(options => options.AppendTrailingSlash = true);

in Startup.ConfigureServices without success, meaning that the request still does not have the trailing slash through SwaggerUi.


How do I enforce trailing slashes or can I do something else to make sure that the FromBody parameter is working?

  • 1
    The problem is not due to a trailing slash or lack thereof. That's a red herring. If the value is coming through as null, then that means that it is not being submitted in correct format. In particular, `FromBody` means your request body needs to have a content type of something like `application/json`, or `application/xml`. If you're trying to do x-www-form-urlencoded or multipart/form-data, then you should be using `FromForm` instead. – Chris Pratt Sep 07 '17 at 15:23
  • Is it a problem with your web api controller, the generated spec or swagger ui? – Rico Suter Sep 08 '17 at 06:46
  • Have you tried [HttpPost()] without empty string? – Rico Suter Sep 08 '17 at 06:50
  • @ChrisPratt Both set the content type to application/json. –  Sep 08 '17 at 07:27
  • @RicoSuter I think the problem is with the generated spec or the ui. The controller works, because it works with Postman. I tried HttpPost without empty string. –  Sep 08 '17 at 07:29
  • @RicoSuter The generated client from NSwag is working in another .Net project, although the missing trailing slash. So something else is off here. I think SwaggerUi is the problem then. –  Sep 08 '17 at 10:56
  • Strange, I will soon upgrade NSwag to Swagger UI 3.x (currently 2.x), maybe this fixes the problem... – Rico Suter Sep 08 '17 at 11:07
  • @RicoSuter sounds good. Do you have a timeframe for SwaggerUi 3? I just introduced members of my development team to NSwag and I think they would love that :) –  Sep 08 '17 at 11:43
  • 1
    Its already working in a feature branch: https://github.com/RSuter/NSwag/tree/redoc-and-new-swagger-ui – Rico Suter Sep 08 '17 at 12:08
  • Now merged into mastrr, UseSwaggerUi3() and UseSwaggerReDoc() – Rico Suter Sep 13 '17 at 21:43
  • Btw: Have you checked chris' comment? If the transmitted json is invalid, the obj is null... try using a simpler object to test this... – Rico Suter Sep 14 '17 at 16:37
  • The item is just three strings. And the exact same json is working in Postman @RicoSuter. The current Master is not released yet, right? –  Sep 15 '17 at 07:00
  • Ah ok, then its probably really a problem with Swagger UI... No its not released yet – Rico Suter Sep 15 '17 at 10:59
  • The NSwag v11.7.2 release build is running now, should be available soon – Rico Suter Sep 15 '17 at 15:36
  • Is this issue resolved? – Rico Suter Nov 01 '17 at 09:05

0 Answers0