Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 BodyParser doesn't detect chartset content-types without spaces #650

Closed
eduardohitek opened this issue Jul 19, 2020 · 10 comments
Closed

Comments

@eduardohitek
Copy link

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"
@welcome
Copy link

welcome bot commented Jul 19, 2020

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

@ReneWerner87
Copy link
Member

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:
d61e478#diff-8500aba9f2dd08390bcbe6a506b66564R232

https://github.com/gofiber/fiber/blob/master/utils.go#L305
https://github.com/gofiber/fiber/blob/master/utils.go#L315

@eduardohitek
Copy link
Author

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.

@jos-
Copy link

jos- commented Jul 21, 2020

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.

@eduardohitek
Copy link
Author

@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
})

@Fenny
Copy link
Member

Fenny commented Jul 21, 2020

@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 #655

Thanks guys for the reports, should be tagged this week 馃憤

@jos-
Copy link

jos- commented Jul 21, 2020

@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.

@Fenny Fenny changed the title c.BodyParser can't parse content-type: application/json;charset=utf-8 馃悰 BodyParser doesn't detect chartset content-types without spaces Jul 21, 2020
@Fenny
Copy link
Member

Fenny commented Jul 21, 2020

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

@Fenny Fenny closed this as completed Jul 21, 2020
@jos-
Copy link

jos- commented Jul 22, 2020

Thanks for the quick fix. Thanks for this great framework 馃憤

@eduardohitek
Copy link
Author

Thank u guys!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants