From 9150958044c4dabcecbd419aff2fe74b45f8beab Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sat, 3 Apr 2021 11:55:22 +0300 Subject: [PATCH] fix x-www-form-urlencoded requests Signed-off-by: Vasiliy Tolstov --- go.mod | 2 +- go.sum | 6 +++--- http.go | 21 ++++++++++++++------- util.go | 4 ++++ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index 10f3fa1..6e5e944 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/unistack-org/micro-client-http/v3 go 1.16 -require github.com/unistack-org/micro/v3 v3.3.10 +require github.com/unistack-org/micro/v3 v3.3.11 diff --git a/go.sum b/go.sum index 61d66a3..e50bf58 100644 --- a/go.sum +++ b/go.sum @@ -5,9 +5,9 @@ github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/silas/dag v0.0.0-20210121180416-41cf55125c34/go.mod h1:7RTUFBdIRC9nZ7/3RyRNH1bdqIShrDejd1YbLwgPS+I= -github.com/unistack-org/micro/v3 v3.3.10 h1:yMSiyplupFQ7xy/kNXL/zqSnqGSjrlNa2GTCwea5BVg= -github.com/unistack-org/micro/v3 v3.3.10/go.mod h1:5ragE2E8ER5d4FZQJG9pB6qdfOoLXLfKW89l77Dy3jQ= -golang.org/x/net v0.0.0-20210331060903-cb1fcc7394e5/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +github.com/unistack-org/micro/v3 v3.3.11 h1:Jr0gAw5lLqgddiHKQeWUOUeP6ZqgRhz52EA9zJ5MJ3U= +github.com/unistack-org/micro/v3 v3.3.11/go.mod h1:PPCt675o3HPcODFbJ4iRWPmQFAk1WQ+asQSOb/syq6U= +golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/http.go b/http.go index 5c07922..9e268fd 100644 --- a/http.go +++ b/http.go @@ -19,6 +19,7 @@ import ( "github.com/unistack-org/micro/v3/errors" "github.com/unistack-org/micro/v3/metadata" "github.com/unistack-org/micro/v3/router" + rutil "github.com/unistack-org/micro/v3/util/reflect" ) var ( @@ -100,12 +101,18 @@ func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg var b []byte - if ct == "x-www-form-urlencoded" { - fmt.Printf("XXXXX %#+v\n", nmsg) - } else if nmsg != nil { - b, err = cf.Marshal(nmsg) - if err != nil { - return nil, errors.BadRequest("go.micro.client", err.Error()) + if nmsg != nil { + if ct == "application/x-www-form-urlencoded" { + data, err := rutil.StructURLValues(nmsg, "", tags) + if err != nil { + return nil, errors.BadRequest("go.micro.client", err.Error()) + } + b = []byte(data.Encode()) + } else { + b, err = cf.Marshal(nmsg) + if err != nil { + return nil, errors.BadRequest("go.micro.client", err.Error()) + } } } @@ -136,7 +143,7 @@ func (h *httpClient) call(ctx context.Context, addr string, req client.Request, var err error // get codec switch ct { - case "x-www-form-urlencoded": + case "application/x-www-form-urlencoded": cf, err = h.newCodec(DefaultContentType) default: cf, err = h.newCodec(ct) diff --git a/util.go b/util.go index 58423e0..cd7ab8f 100644 --- a/util.go +++ b/util.go @@ -87,6 +87,10 @@ func newPathRequest(path string, method string, body string, msg interface{}, ta t.name = strings.ToLower(fld.Name) } + if !val.IsValid() || val.IsZero() { + continue + } + if _, ok := fieldsmap[t.name]; ok { fieldsmap[t.name] = fmt.Sprintf("%v", val.Interface()) } else if (body == "*" || body == t.name) && method != http.MethodGet {