allow to response with different content-type
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
919bf1486f
commit
d3d67eca97
26
handler.go
26
handler.go
@ -243,7 +243,22 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
scode := int(200)
|
scode := int(200)
|
||||||
if appErr := fn(ctx, hr, replyv.Interface()); appErr != nil {
|
appErr := fn(ctx, hr, replyv.Interface())
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", ct)
|
||||||
|
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
||||||
|
for k, v := range md {
|
||||||
|
w.Header().Set(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ct != w.Header().Get("Content-Type") {
|
||||||
|
if cf, err = h.newCodec(ct); err != nil {
|
||||||
|
h.errorHandler(ctx, nil, w, r, err, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if appErr != nil {
|
||||||
switch verr := appErr.(type) {
|
switch verr := appErr.(type) {
|
||||||
case *errors.Error:
|
case *errors.Error:
|
||||||
scode = int(verr.Code)
|
scode = int(verr.Code)
|
||||||
@ -256,19 +271,12 @@ func (h *httpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
b, err = cf.Marshal(replyv.Interface())
|
b, err = cf.Marshal(replyv.Interface())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) {
|
if err != nil && handler.sopts.Logger.V(logger.ErrorLevel) {
|
||||||
handler.sopts.Logger.Errorf(handler.sopts.Context, "handler err: %v", err)
|
handler.sopts.Logger.Errorf(handler.sopts.Context, "handler err: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", ct)
|
|
||||||
|
|
||||||
if md, ok := metadata.FromOutgoingContext(ctx); ok {
|
|
||||||
for k, v := range md {
|
|
||||||
w.Header().Set(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if nscode := GetRspCode(ctx); nscode != 0 {
|
if nscode := GetRspCode(ctx); nscode != 0 {
|
||||||
scode = nscode
|
scode = nscode
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user