dcsunny 4 年之前
父节点
当前提交
620c62f3ab
共有 2 个文件被更改,包括 16 次插入2 次删除
  1. 13 1
      http/context/context.go
  2. 3 1
      http/handle.go

+ 13 - 1
http/context/context.go

@@ -9,6 +9,18 @@ import (
 
 func AppendToContext(ctx context.Context, key string, value interface{}) context.Context {
 	ctx = context.WithValue(ctx, key, value)
-	ctx = metadata.AppendToOutgoingContext(ctx, key, fmt.Sprint(value))
+	_value := ""
+	switch value.(type) {
+	case []byte:
+		_value = string(value.([]byte))
+		break
+	case string:
+		_value = value.(string)
+		break
+	default:
+		_value = fmt.Sprint(value)
+		break
+	}
+	ctx = metadata.AppendToOutgoingContext(ctx, key, _value)
 	return ctx
 }

+ 3 - 1
http/handle.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"context"
 	json2 "encoding/json"
+	"fmt"
 	"io/ioutil"
 	"net/http"
 	"strings"
@@ -217,7 +218,8 @@ func contentSubtype(contentType string) string {
 func SetBody(ctx context.Context, r *http.Request) context.Context {
 	b, _ := ioutil.ReadAll(r.Body)
 	if len(b) > 0 {
-		ctx = context2.AppendToContext(ctx, "body", string(b))
+		fmt.Println(string(b))
+		ctx = context2.AppendToContext(ctx, "body", b)
 	}
 	return ctx
 }