Robin Kaltenbach
Robin Kaltenbach

Reputation: 178

ASP .NET Core Endpoint returns different Content Types

While testing my endpoints I recognized that an endpoint returned different Content Types when sending different input values. For instance: The endpoint returns a Supplier when an ID is given. If the HTTP Status Code is 200 the content type is "application/json; charset=utf-8; v=1.0". But when I submit bullshit like a random string instead of an integer, obviously the api returns a HTTP 400. The content type now is "application/json; charset=utf-8". The "v=1.0" is missing. It seems trivial but our integration tests check if the Content Type is like the expected application/json; charset=utf-8; v=1.0.

Also, we defined in the Startup.cs: options.DefaultApiVersion = new ApiVersion(1, 0);

Endpoint code:

[HttpGet("{id:int}")]
public async Task<IActionResult> GetById(int id)
{
    var body = await _lieferantenService.GetById(id);
    return Ok(body);
}

Does anybody encountered this kind of problem?

Upvotes: 1

Views: 384

Answers (1)

Jehof
Jehof

Reputation: 35544

It is most likely that the response comes from a middleware and not from your code.

I think the middleware that validates input or the one that is responsible for model bindings. (Mapping bullshit to integer).

To customize the Response use InvalidModelStateResponseFactory of ApiBehaviorOptions.

See answer to this question. https://stackoverflow.com/a/51442067/83039.

Upvotes: 1

Related Questions