From cef83efd4e232f99f38ad3fb473f3f5d165d6ef1 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Thu, 28 Apr 2022 11:25:26 +0800 Subject: [PATCH] fix #1838 (#1839) --- core/stat/remotewriter.go | 7 +++++-- rest/httpc/vars.go | 2 +- rest/httpx/vars.go | 2 +- tools/goctl/api/dartgen/vars.go | 2 +- tools/goctl/api/ktgen/apibase.tpl | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/core/stat/remotewriter.go b/core/stat/remotewriter.go index 65469ea1..b742e48d 100644 --- a/core/stat/remotewriter.go +++ b/core/stat/remotewriter.go @@ -10,7 +10,10 @@ import ( "github.com/zeromicro/go-zero/core/logx" ) -const httpTimeout = time.Second * 5 +const ( + httpTimeout = time.Second * 5 + jsonContentType = "application/json; charset=utf-8" +) // ErrWriteFailed is an error that indicates failed to submit a StatReport. var ErrWriteFailed = errors.New("submit failed") @@ -36,7 +39,7 @@ func (rw *RemoteWriter) Write(report *StatReport) error { client := &http.Client{ Timeout: httpTimeout, } - resp, err := client.Post(rw.endpoint, "application/json", bytes.NewBuffer(bs)) + resp, err := client.Post(rw.endpoint, jsonContentType, bytes.NewBuffer(bs)) if err != nil { return err } diff --git a/rest/httpc/vars.go b/rest/httpc/vars.go index 03349774..5f318dd7 100644 --- a/rest/httpc/vars.go +++ b/rest/httpc/vars.go @@ -10,7 +10,7 @@ const ( slash = "/" colon = ':' contentType = "Content-Type" - applicationJson = "application/json" + applicationJson = "application/json; charset=utf-8" ) // ErrGetWithBody indicates that GET request with body. diff --git a/rest/httpx/vars.go b/rest/httpx/vars.go index a7cb8f20..1604727f 100644 --- a/rest/httpx/vars.go +++ b/rest/httpx/vars.go @@ -2,7 +2,7 @@ package httpx const ( // ApplicationJson means application/json. - ApplicationJson = "application/json" + ApplicationJson = "application/json; charset=utf-8" // ContentEncoding means Content-Encoding. ContentEncoding = "Content-Encoding" // ContentSecurity means X-Content-Security. diff --git a/tools/goctl/api/dartgen/vars.go b/tools/goctl/api/dartgen/vars.go index 621a7c3a..68cbaa82 100644 --- a/tools/goctl/api/dartgen/vars.go +++ b/tools/goctl/api/dartgen/vars.go @@ -58,7 +58,7 @@ Future _apiRequest(String method, String path, dynamic data, r = await client.getUrl(Uri.parse('https://' + serverHost + path)); } - r.headers.set('Content-Type', 'application/json'); + r.headers.set('Content-Type', 'application/json; charset=utf-8'); if (tokens != null) { r.headers.set('Authorization', tokens.accessToken); } diff --git a/tools/goctl/api/ktgen/apibase.tpl b/tools/goctl/api/ktgen/apibase.tpl index 4aff68a5..6326abe6 100644 --- a/tools/goctl/api/ktgen/apibase.tpl +++ b/tools/goctl/api/ktgen/apibase.tpl @@ -25,7 +25,7 @@ suspend fun apiRequest( requestMethod = method doInput = true if (method == "POST" || method == "PUT" || method == "PATCH") { - setRequestProperty("Content-Type", "application/json") + setRequestProperty("Content-Type", "application/json; charset=utf-8") doOutput = true val data = when (body) { is String -> {