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

[ISSUE #461] fix response header Content-Type #462

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/sample/http/http-grpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,6 @@ and

```
curl http://127.0.0.1:8881/api/v1/provider.UserProvider/GetUser -X POST -d '{"userId":1}'
```
```

> 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'.
8 changes: 5 additions & 3 deletions pkg/common/constant/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "://"
Expand Down
7 changes: 6 additions & 1 deletion pkg/common/http/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package http

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
stdHttp "net/http"
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions pkg/context/http/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package http

import (
"context"
"encoding/json"
"math"
"net"
"net/http"
Expand Down Expand Up @@ -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)
Expand Down