rpmansion
rpmansion

Reputation: 2014

NSwag default accept response to application/json

I'm using NSwag with an Asp.Net Core API. I would like to change the default accepted response from application/octet-stream to application/json.

enter image description here

I tried adding Response attribute to the API action and also the OpenApiMimeType but did not work. It is still set to application/octet-stream. How do I change this?

Update: Here is one of the actions for the API.

[HttpPost("login")]
public async Task<ActionResult> Login(LoginDto dto, string returnUrl = null)
{
    //... omitted codes for brevity
    
    return Ok();
}

Upvotes: 2

Views: 1881

Answers (1)

Sanrita
Sanrita

Reputation: 19

Using the Produce attribute (https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.producesattribute) should help you achieve this.

[HttpPost("login")]
[Produces("application/json")]
public async Task<ActionResult> Login(LoginDto dto, string returnUrl = null)
{
    //... omitted codes for brevity
    
    return Ok();
}

In your swagger.json it should produce something like this: Swagger.json - 1

The other reason is because you are using Task<ActionResult> so your swagger.json isn't producing a content type due to this reason.

[HttpPost("login")]
[Produces("application/json")]
public async Task<ActionResult<return something>> Login(LoginDto dto, string returnUrl = null)
{
    //... omitted codes for brevity
    
    return Ok(return something);
}

Upvotes: 1

Related Questions