FormatException in NSwag for .NET When POST Method Has No Body

38 views Asked by At

I'm encountering a FormatException in a .NET application using NSwag when calling a POST method without a body. The problematic endpoint is defined as follows:

[HttpPost]
[Route("AddUser/{id}")]
[EnableCors]
[AllowAnonymous]
public async Task<ActionResult<Response>> AddUser([FromRoute] string id)

When NSwag processes this endpoint via the resulting swagger.json, it generates the following line in swaggerclient.cs:

request_.Content = new System.Net.Http.StringContent(string.Empty, System.Text.Encoding.UTF8, "application/json;charset=utf-8");

Executing this line triggers a FormatException. What could be causing this issue, and how can I resolve it? Is this a problem with NSwag's handling of Swagger/OpenAPI specs, or is there a specific configuration required for endpoints without a body in NSwag?

I should also note that specifying [Consumes] tag does not seem to alter the resulting swagger.json via swashbuckle unless I specify some input using [FromBody] tag.

More information about this problem can be found here but since it dates back to 2019, I'm not sure if I can't find a fix or not.

0

There are 0 answers