1
votes

There is a service written in golang, using gin-gonic framework.

I only ever want to support application/json as a mime-type and it would be great if it always be in UTF-8. The business logic of the service might break if it will get values in different encodings.

Is it a good idea to write a custom middleware that checks if Content-Type header has value "application/json; charset=utf-8" and returns some 4xx status in case it is not?

UPDATE: Just found out that ctx.ContentType() doesn't return charset part of the header. Is there a way to get it?

1
Yes it is good.Зелёный

1 Answers

2
votes

Nothing prevents you from simply looking at the "Content-Type" http header directly, to the tune of ctx.Request.Header.Get("Content-Type").

The helper ContentType method is provided by gin-gonic especially for the rather common case of querying the "unadulterated" mime type of incoming data without too much hassle.