Fiber: 馃悰 BodyParser doesn't detect chartset content-types without spaces

Created on 19 Jul 2020  路  10Comments  路  Source: gofiber/fiber

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"
馃彿 Wait for Release

Most helpful comment

@jos- , @eduardohitek fixed in https://github.com/gofiber/fiber/releases/tag/v1.13.3 馃憤

All 10 comments

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.

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!!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

petersephrin picture petersephrin  路  4Comments

GrigoriyMikhalkin picture GrigoriyMikhalkin  路  4Comments

MohamedGouaouri picture MohamedGouaouri  路  3Comments

abowloflrf picture abowloflrf  路  4Comments

ahan picture ahan  路  3Comments