Repository: cloudwego/hertz
Pull request #1369 (2025-06-05 05:46:31)

fix(sse): add charset=utf-8 to content type
#### What type of PR is this?
fix

<!--
Add one of the following kinds:

build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
docs: Documentation only changes
feat: A new feature
optimize: A new optimization
fix: A bug fix
perf: A code change that improves performance
refactor: A code change that neither fixes a bug nor adds a feature
style: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc)
test: Adding missing tests or correcting existing tests
chore: Changes to the build process or auxiliary tools and libraries such as documentation generation
-->

#### Check the PR title.
<!--
The description of the title will be attached in Release Notes, 
so please describe it from user-oriented, what this PR does / why we need it.
Please check your PR title with the below requirements:
-->
- [ ] This PR title match the format: \<type\>(optional scope): \<description\>
- [ ] The description of this PR title is user-oriented and clear enough for others to understand.
- [ ] Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. [User docs repo](https://github.com/cloudwego/cloudwego.github.io)


#### (Optional) Translate the PR title into Chinese.


#### (Optional) More detailed description for this PR(en: English/zh: Chinese).
<!--
Provide more detailed info for review(e.g., it's recommended to provide perf data if this is a perf type PR).
-->
en:
zh(optional):

#### (Optional) Which issue(s) this PR fixes:
<!--
Automatically closes linked issue when PR is merged.
Eg: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->

#### (Optional) The PR that updates user documentation:
<!--
If the current PR requires user awareness at the usage level, please submit a PR to update user docs. [User docs repo](https://github.com/cloudwego/cloudwego.github.io)
-->

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/cloudwego/hertz/pull/1369?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 73.99%. Comparing base [(`c0dc541`)](https://app.codecov.io/gh/cloudwego/hertz/commit/c0dc541e7a72f49e64e6a2a79b0779f7abe05b3d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego) to head [(`22a9a9b`)](https://app.codecov.io/gh/cloudwego/hertz/commit/22a9a9b438ad53995010e4d9de2929a5d2ffdfe0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           develop    #1369      +/-   ##
===========================================
+ Coverage    73.96%   73.99%   +0.02%     
===========================================
  Files          145      145              
  Lines        16906    16909       +3     
===========================================
+ Hits         12504    12511       +7     
+ Misses        3806     3803       -3     
+ Partials       596      595       -1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/cloudwego/hertz/pull/1369?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cloudwego).

<details><summary> :rocket: New features to boost your workflow: </summary>

- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>

Patch:
diff --git a/pkg/protocol/sse/writer.go b/pkg/protocol/sse/writer.go
index 511eecb..c910a5a 100644
--- a/pkg/protocol/sse/writer.go
+++ b/pkg/protocol/sse/writer.go
@@ -23,7 +23,6 @@ import (
 	"strings"
 	"sync"
 
-	"github.com/cloudwego/hertz/internal/bytestr"
 	"github.com/cloudwego/hertz/pkg/app"
 	"github.com/cloudwego/hertz/pkg/common/bytebufferpool"
 	"github.com/cloudwego/hertz/pkg/network"
@@ -41,8 +40,11 @@ type Writer struct {
 
 // NewWriter creates a new SSE writer.
 func NewWriter(c *app.RequestContext) *Writer {
+	// make sure proxies won't cache the data
 	c.Response.Header.Set("Cache-Control", "no-cache")
-	c.Response.Header.SetContentType(string(bytestr.MIMETextEventStream))
+	// browsers may need charset=utf-8 for logging responses
+	// even though it's unnecessary as per spec, coz chunks must be in utf8.
+	c.Response.Header.SetContentType("text/event-stream; charset=utf-8")
 	w := c.Response.GetHijackWriter()
 	if w == nil {
 		w = resp.NewChunkedBodyWriter(&c.Response, c.GetWriter())


Test patch:
diff --git a/pkg/protocol/sse/writer_test.go b/pkg/protocol/sse/writer_test.go
index 3c2c693..f27d2d9 100644
--- a/pkg/protocol/sse/writer_test.go
+++ b/pkg/protocol/sse/writer_test.go
@@ -198,7 +198,7 @@ func TestNewWriter(t *testing.T) {
 	w := NewWriter(c)
 	assert.Assert(t, w != nil)
 	assert.DeepEqual(t, "no-cache", string(c.Response.Header.Peek("Cache-Control")))
-	assert.DeepEqual(t, "text/event-stream", string(c.Response.Header.Peek("Content-Type")))
+	assert.DeepEqual(t, "text/event-stream; charset=utf-8", string(c.Response.Header.Peek("Content-Type")))
 }
 
 func TestWriter_WriteComment(t *testing.T) {