|
|
@@ -1,9 +1,13 @@
|
|
|
package http
|
|
|
|
|
|
import (
|
|
|
+ "io/ioutil"
|
|
|
"net/http"
|
|
|
"strings"
|
|
|
|
|
|
+ "github.com/go-kratos/kratos/v2/transport/http/binding"
|
|
|
+ "google.golang.org/protobuf/types/known/emptypb"
|
|
|
+
|
|
|
"github.com/go-kratos/kratos/v2/encoding"
|
|
|
"github.com/go-kratos/kratos/v2/errors"
|
|
|
|
|
|
@@ -12,23 +16,23 @@ import (
|
|
|
)
|
|
|
|
|
|
// decodeRequest decodes the request body to object.
|
|
|
-//func DecodeRequest(req *http.Request, v interface{}) error {
|
|
|
-// method := strings.ToUpper(req.Method)
|
|
|
-// if method == "POST" || method == "PUT" || method == "DELETE" {
|
|
|
-// if _, ok := v.(*emptypb.Empty); ok {
|
|
|
-// return binding.BindForm(req, v)
|
|
|
-// }
|
|
|
-// subtype := contentSubtype(req.Header.Get(ContentTypeHeader))
|
|
|
-// if codec := encoding.GetCodec(subtype); codec != nil {
|
|
|
-// data, err := ioutil.ReadAll(req.Body)
|
|
|
-// if err != nil {
|
|
|
-// return err
|
|
|
-// }
|
|
|
-// return codec.Unmarshal(data, v)
|
|
|
-// }
|
|
|
-// }
|
|
|
-// return binding.BindForm(req, v)
|
|
|
-//}
|
|
|
+func DecodeRequest(req *http.Request, v interface{}) error {
|
|
|
+ method := strings.ToUpper(req.Method)
|
|
|
+ if method == "POST" || method == "PUT" || method == "DELETE" {
|
|
|
+ if _, ok := v.(*emptypb.Empty); ok {
|
|
|
+ return binding.BindForm(req, v)
|
|
|
+ }
|
|
|
+ subtype := contentSubtype(req.Header.Get(ContentTypeHeader))
|
|
|
+ if codec := encoding.GetCodec(subtype); codec != nil {
|
|
|
+ data, err := ioutil.ReadAll(req.Body)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return codec.Unmarshal(data, v)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return binding.BindForm(req, v)
|
|
|
+}
|
|
|
|
|
|
func ErrHandle(w http.ResponseWriter, r *http.Request, err error) {
|
|
|
se, ok := errors.FromError(err)
|