This is an automated email from the ASF dual-hosted git repository.
alexstocks pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go-pixiu.git
The following commit(s) were added to refs/heads/develop by this push:
new ce9145a3 fix response header Content-Type
new cbf006f2 Merge pull request #462 from victory460/feature_content_type
ce9145a3 is described below
commit ce9145a37f74ff31493d6db351f86eb5e5a10563
Author: xuweiwei <[email protected]>
AuthorDate: Tue Aug 2 22:52:35 2022 +0800
fix response header Content-Type
---
docs/sample/http/http-grpc.md | 4 +++-
pkg/common/constant/http.go | 8 +++++---
pkg/common/http/manager.go | 7 ++++++-
pkg/context/http/context.go | 8 ++++++--
4 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/docs/sample/http/http-grpc.md b/docs/sample/http/http-grpc.md
index 3dfaaf7d..806929c9 100644
--- a/docs/sample/http/http-grpc.md
+++ b/docs/sample/http/http-grpc.md
@@ -103,4 +103,6 @@ and
```
curl http://127.0.0.1:8881/api/v1/provider.UserProvider/GetUser -X POST -d
'{"userId":1}'
-```
\ No newline at end of file
+```
+
+> If response body is a json, the header of 'content-type' will set to
'application/json'. If it is just a plain text, the header of 'content-type' is
'text/plain'.
\ No newline at end of file
diff --git a/pkg/common/constant/http.go b/pkg/common/constant/http.go
index 9a58f813..69fa073f 100644
--- a/pkg/common/constant/http.go
+++ b/pkg/common/constant/http.go
@@ -26,9 +26,11 @@ const (
HeaderKeyAccessControlMaxAge = "Access-Control-Max-Age"
HeaderKeyAccessControlAllowCredentials =
"Access-Control-Allow-Credentials"
- HeaderValueJsonUtf8 = "application/json;charset=UTF-8"
- HeaderValueTextPlain = "text/plain"
- HeaderValueAll = "*"
+ HeaderValueJsonUtf8 = "application/json;charset=UTF-8"
+ HeaderValueTextPlain = "text/plain"
+ HeaderValueApplicationJson = "application/json"
+
+ HeaderValueAll = "*"
PathSlash = "/"
ProtocolSlash = "://"
diff --git a/pkg/common/http/manager.go b/pkg/common/http/manager.go
index dc09679a..eb7e66b2 100644
--- a/pkg/common/http/manager.go
+++ b/pkg/common/http/manager.go
@@ -19,6 +19,7 @@ package http
import (
"context"
+ "encoding/json"
"fmt"
"io/ioutil"
stdHttp "net/http"
@@ -141,7 +142,11 @@ func (hcm *HttpConnectionManager) buildTargetResponse(c
*pch.HttpContext) {
c.TargetResp = &client.Response{Data: body}
case []byte:
c.StatusCode(stdHttp.StatusOK)
- c.AddHeader(constant.HeaderKeyContextType,
constant.HeaderValueTextPlain)
+ if json.Valid(res) {
+ c.AddHeader(constant.HeaderKeyContextType,
constant.HeaderValueApplicationJson)
+ } else {
+ c.AddHeader(constant.HeaderKeyContextType,
constant.HeaderValueTextPlain)
+ }
c.TargetResp = &client.Response{Data: res}
default:
//dubbo go generic invoke
diff --git a/pkg/context/http/context.go b/pkg/context/http/context.go
index 6e7d7198..4b87123d 100644
--- a/pkg/context/http/context.go
+++ b/pkg/context/http/context.go
@@ -19,6 +19,7 @@ package http
import (
"context"
+ "encoding/json"
"math"
"net"
"net/http"
@@ -177,8 +178,11 @@ func (hc *HttpContext) SendLocalReply(status int, body
[]byte) {
hc.statusCode = status
hc.localReplyBody = body
hc.TargetResp = &client.Response{Data: body}
- hc.AddHeader(constant.HeaderKeyContextType,
constant.HeaderValueTextPlain)
-
+ if json.Valid(body) {
+ hc.AddHeader(constant.HeaderKeyContextType,
constant.HeaderValueApplicationJson)
+ } else {
+ hc.AddHeader(constant.HeaderKeyContextType,
constant.HeaderValueTextPlain)
+ }
writer := hc.Writer
writer.WriteHeader(status)
_, err := writer.Write(body)