Merge pull request #462 from victory460/feature_content_type
[ISSUE #461] fix response header Content-Type
diff --git a/docs/sample/http/http-grpc.md b/docs/sample/http/http-grpc.md
index 3dfaaf7..806929c 100644
--- a/docs/sample/http/http-grpc.md
+++ b/docs/sample/http/http-grpc.md
@@ -103,4 +103,6 @@
```
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 9a58f81..69fa073 100644
--- a/pkg/common/constant/http.go
+++ b/pkg/common/constant/http.go
@@ -26,9 +26,11 @@
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 dc09679..eb7e66b 100644
--- a/pkg/common/http/manager.go
+++ b/pkg/common/http/manager.go
@@ -19,6 +19,7 @@
import (
"context"
+ "encoding/json"
"fmt"
"io/ioutil"
stdHttp "net/http"
@@ -141,7 +142,11 @@
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 6e7d719..4b87123 100644
--- a/pkg/context/http/context.go
+++ b/pkg/context/http/context.go
@@ -19,6 +19,7 @@
import (
"context"
+ "encoding/json"
"math"
"net"
"net/http"
@@ -177,8 +178,11 @@
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)