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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove default charset from 'application/json' Content-Type header #2568

Merged
merged 1 commit into from Feb 6, 2024

Conversation

doortts
Copy link
Contributor

@doortts doortts commented Dec 28, 2023

Using application/json; charset=UTF-8 in response header is a common misuse. I think it is better to remove ; charset=UTF-8 from default json response Content-Type header to prevent the misconception.

See: #2567

Copy link
Contributor

@aldas aldas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can not just remove/replace echo.MIMEApplicationJSONCharsetUTF8 as it is part of Echos public API and we are quite strict on API changes.

  • it would be nice if there would be link to JSON spec and remark about charset in doccomment

other than that I think this is probably ok

@doortts
Copy link
Contributor Author

doortts commented Dec 29, 2023

@aldas Thank you for the comment.

echo.go Show resolved Hide resolved
@doortts doortts force-pushed the default-json-encoding branch 2 times, most recently from 40b7cab to aa3d3cb Compare December 29, 2023 18:27
@aldas aldas merged commit 76994d1 into labstack:master Feb 6, 2024
@aldas
Copy link
Contributor

aldas commented Feb 6, 2024

done, Thanks @doortts

nono added a commit to cozy/cozy-stack that referenced this pull request Apr 22, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/labstack/echo/v4](https://togithub.com/labstack/echo) |
`v4.11.4` -> `v4.12.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2flabstack%2fecho%2fv4/v4.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2flabstack%2fecho%2fv4/v4.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2flabstack%2fecho%2fv4/v4.11.4/v4.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2flabstack%2fecho%2fv4/v4.11.4/v4.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>labstack/echo (github.com/labstack/echo/v4)</summary>

###
[`v4.12.0`](https://togithub.com/labstack/echo/blob/HEAD/CHANGELOG.md#v4120---2024-04-15)

[Compare
Source](https://togithub.com/labstack/echo/compare/v4.11.4...v4.12.0)

**Security**

- Update golang.org/x/net dep because of
[GO-2024-2687](https://pkg.go.dev/vuln/GO-2024-2687) by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2625

**Enhancements**

- binder: make binding to Map work better with string destinations by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2554
- README.md: add Encore as sponsor by
[@&#8203;marcuskohlberg](https://togithub.com/marcuskohlberg) in
[labstack/echo#2579
- Reorder paragraphs in README.md by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2581
- CI: upgrade actions/checkout to v4 by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2584
- Remove default charset from 'application/json' Content-Type header by
[@&#8203;doortts](https://togithub.com/doortts) in
[labstack/echo#2568
- CI: Use Go 1.22 by [@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2588
- binder: allow binding to a nil map by
[@&#8203;georgmu](https://togithub.com/georgmu) in
[labstack/echo#2574
- Add Skipper Unit Test In BasicBasicAuthConfig and Add More Detail
Explanation regarding BasicAuthValidator by
[@&#8203;RyoKusnadi](https://togithub.com/RyoKusnadi) in
[labstack/echo#2461
- fix some typos by
[@&#8203;teslaedison](https://togithub.com/teslaedison) in
[labstack/echo#2603
- fix: some typos by [@&#8203;pomadev](https://togithub.com/pomadev) in
[labstack/echo#2596
- Allow ResponseWriters to unwrap writers when flushing/hijacking by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2595
- Add SPDX licence comments to files. by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2604
- Upgrade deps by [@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2605
- Change type definition blocks to single declarations. This helps copy…
by [@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2606
- Fix Real IP logic by [@&#8203;cl-bvl](https://togithub.com/cl-bvl) in
[labstack/echo#2550
- Default binder can use `UnmarshalParams(params []string) error` inter…
by [@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2607
- Default binder can bind pointer to slice as struct field. For example
`*[]string` by [@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2608
- Remove maxparam dependence from Context by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2611
- When route is registered with empty path it is normalized to `/`. by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2616
- proxy middleware should use httputil.ReverseProxy for SSE requests by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2624

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 6am on Monday" in timezone
Europe/Paris, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/cozy/cozy-stack).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zMTMuMSIsInVwZGF0ZWRJblZlciI6IjM3LjMxMy4xIiwidGFyZ2V0QnJhbmNoIjoibWFzdGVyIiwibGFiZWxzIjpbXX0=-->
@washington-guedes
Copy link

washington-guedes commented May 7, 2024

There are clients outside of what was called "compliant recipient" that got broken, like flutter v2.1 in my case. Is it possible to have a middleware that forces the header Content-Type back to MIMEApplicationJSONCharsetUTF8? @aldas @doortts @nono

@aldas
Copy link
Contributor

aldas commented May 7, 2024

There are clients outside of what was called "compliant recipient" that got broken, like flutter v2.1 in my case. Is it possible to have a middleware that forces the header Content-Type back to MIMEApplicationJSONCharsetUTF8? @aldas @doortts @nono

Try

	e.GET("/", func(c echo.Context) error {
		c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
		return c.JSON(http.StatusOK, "HI")
	})

This is how header is written for c.JSON - only when there is no content type header we set it as application/json

echo/context.go

Lines 243 to 248 in 88c379f

func (c *context) writeContentType(value string) {
header := c.Response().Header()
if header.Get(HeaderContentType) == "" {
header.Set(HeaderContentType, value)
}
}

Example

func main() {
	e := echo.New()

	e.GET("/", func(c echo.Context) error {
		c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
		return c.JSON(http.StatusOK, "HI")
	})

	if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatal(err)
	}
}

Output:

x@x:~/$ curl -v http://localhost:8080
* processing: http://localhost:8080
*   Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.2.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=UTF-8
< Date: Tue, 07 May 2024 18:44:09 GMT
< Content-Length: 5
< 
"HI"
* Connection #0 to host localhost left intact

@aldas
Copy link
Contributor

aldas commented May 7, 2024

or you could create custom json serializer than sets that header before writing the json

package main

import (
	"encoding/json"
	"errors"
	"github.com/labstack/echo/v4"
	"log"
	"net/http"
)

type utf8JSONSerializer struct {
	echo.DefaultJSONSerializer
}

func (d utf8JSONSerializer) Serialize(c echo.Context, i interface{}, indent string) error {
	c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
	return d.DefaultJSONSerializer.Serialize(c, i, indent)
}

func main() {
	e := echo.New()
	e.JSONSerializer = new(utf8JSONSerializer)

	e.GET("/", func(c echo.Context) error {
		return c.JSON(http.StatusOK, "HI")
	})

	if err := e.Start(":8080"); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatal(err)
	}
}

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

Successfully merging this pull request may close these issues.

None yet

3 participants