ikyriak / IdempotentAPI

A .NET library that handles the HTTP write operations (POST and PATCH) that can affect only once for the given request data and idempotency-key by using an ASP.NET Core attribute (filter).
MIT License
253 stars 38 forks source link

What do you do with Content-Type caching? #49

Closed koide closed 1 year ago

koide commented 1 year ago

By default .NET 6.0 sets Content-Type to "application/json; charset=utf-8" on response.

But, if we replay this Content-Type header into the request, then no outputformatter matches, because SystemTextJsonOutputFormatter only matches "application/json" without a charset parameter.

This requires either removing the charset when caching, or adding the charset to the outputformatter so it matches.

What do you usually do?

On a separate note, I've been modernizing a bit the code, are you receiving PRs?

ikyriak commented 1 year ago

Hello @koide

Thanks for your message.

I may not understand your question clearly. Is this a general question, or have you found an issue? Currently, we define the request and response content type in the controller as follows.

[ApiController]
[Route("[controller]")]
[Consumes("application/json")] // We should define this.
[Produces("application/json")] // We should define this.
[Idempotent(Enabled = true)]
public class SimpleController : ControllerBase
{
    // ...
}

Yes, I am receiving PRs. So, if you have something to contribute, please create a PR.

Have a nice day,

koide commented 1 year ago

Thanks @ikyriak - if you don't specify [Produces("application/json")] then the framework produces "application/json; charset=utf8" as the response's Content-Type and that leads to a 406 Not Acceptable response when retrieving the response from the cache as that specific Content-Type does not match any output formatter that's registered by default.

Setting the Produces attribute solves the issue cleanly. Thanks!