Fiber v1.13.1
Fiber is unable to parse a application/json;charset=utf-8 content-type. It was working on v1.12.x version.
Code snippet
type DadosLogin struct {
Email string `json:"email"`
Senha string `json:"password"`
}
func recuperarDadosLogin(c *fiber.Ctx) (DadosLogin, error) {
dadosLogin := new(DadosLogin)
erro := c.BodyParser(dadosLogin)
return *dadosLogin, erro
}
"bodyparser: cannot parse content-type: application/json;charset=utf-8"
Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord
thanks for the bug report
problem is that the content-type header of your request is not perfect, there must be a space between the mime type and the charset declaration
your header:
content-type: application/json;charset=utf-8
expected header:
content-type: application/json; charset=utf-8
we have optimized the bodyparser in https://github.com/gofiber/fiber/releases/tag/v1.13.0 and this new behaviour is more strictly, which brings performance
the change:
https://github.com/gofiber/fiber/commit/d61e4785780fb97df19e611cc9d01f73655a3d50#diff-8500aba9f2dd08390bcbe6a506b66564R232
https://github.com/gofiber/fiber/blob/master/utils.go#L305
https://github.com/gofiber/fiber/blob/master/utils.go#L315
Thanks for the clarification. I just changed this in my app and everything works fine now. Hope this will help if anyone else face this situation.
Thanks for the explanation, the extra space fixes it.
However, this causes many issues while the performance gain is minimal. E.g.
application/json;charset=utf-8 (without space) as default Content-Type header, which is currently difficult to change, see https://github.com/axios/axios/issues/2623.UTF-8 (uppercase), which I haven't found a workaround for yet, see e.g. https://github.com/angular/angular.js/issues/11558.Please reopen this, thanks.
@jos- I was able to solve this, setting an interceptor into my axios.
```
axios.interceptors.request.use((config) => {
config.headers['Content-Type'] = 'application/json; charset=utf-8'
return config
})
@eduardohitek _The specification of media types, RFC 2045, does not require a space. Neither does it prohibit a space; this is more implicit, since RFC 2045 refers to the extended BNF as defined in RFC 822, which clarifies this in clause 3.4.2._
After reading the statement above, we will allow missing spaces in the Content-Type in the next tag 馃憣
@jos- we fixed the case sensitive issue in https://github.com/gofiber/fiber/pull/655
Thanks guys for the reports, should be tagged this week 馃憤
@eduardohitek Thanks, I was able to do the same by specifying headers: { 'Content-Type': 'application/json; charset=utf-8' } in the global axios config.
This works in e.g. Firefox, but not in Chrome/Brave/Chromium, which somehow always sets the charset of PUT/POST requests to uppercase UTF-8 when you specify a charset.
@Fenny Thanks for reopening the issue. fiber gives me the error bodyparser: cannot parse content-type: application/json; charset=UTF-8.
@jos- , @eduardohitek fixed in https://github.com/gofiber/fiber/releases/tag/v1.13.3 馃憤
Thanks for the quick fix. Thanks for this great framework 馃憤
Thank u guys!!!!
Most helpful comment
@jos- , @eduardohitek fixed in https://github.com/gofiber/fiber/releases/tag/v1.13.3 馃憤