Skip to content

🐛 [Bug]: UTF8 not enabled by default #2575

@the-hotmann

Description

@the-hotmann

Bug Description

I used Fiber to handle and API Endpoint and take JSON objects with strings and simply store them in a DB.

The strings are getting send as UTF8 encoded (which everything should be).
When I read the DB it shows broken chars like:

M�dchen (should be Mädchen)

The fix was to implement this:

app.Use(func(c *fiber.Ctx) error {
	if c.Is("json") {
		c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSONCharsetUTF8)
	}
	return c.Next()
})

Which I think is a good workaround, but this just works for JSON and also there was no was just setting the encoding to UTF8 without touching the mimetype.

I think Fiber should work with UTF-8 encoding by default, as it can handle mostly everything.

There should be something like this:

app := fiber.New(fiber.Config{
    Encoding: utf-8mb4, // can be `utf-8`, `utf-8mb4`, `iso-8859`, `windows-1251`, `unicode`, `auto`
})

Default should be utf-8. Mostly it is just important, to have a matching encoding on both sides.
auto could automatically detect the encoding on a request per request base - maybe?

How to Reproduce

  1. use fiber to store JSON responses with Umlaute to a DB
  2. display the content with Fiber aswell
  3. notice the broken special chars

Expected Behavior

I was expecting, that Fiber automatically identified which encoding is used/needed, or automatically goes to the most powerful encoding, so nothing breaks.

Code Snippet (optional)

app.Use(func(c *fiber.Ctx) error {
	if c.Is("json") {
		c.Set(fiber.HeaderContentType, fiber.MIMEApplicationJSONCharsetUTF8)
	}
	return c.Next()
})


### Checklist:

- [X] I agree to follow Fiber's [Code of Conduct](https://github.com/gofiber/fiber/blob/master/.github/CODE_OF_CONDUCT.md).
- [X] I have checked for existing issues that describe my problem prior to opening this one.
- [X] I understand that improperly formatted bug reports may be closed without explanation.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions