Compare commits

...

6 Commits

Author SHA1 Message Date
2b16a8a7a6 allow to set content type with charset
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-10 12:46:34 +03:00
Renovate Bot
d051256839 Update module github.com/unistack-org/micro/v3 to v3.3.13 2021-04-10 00:54:11 +00:00
68b32989fc allow to override content-type
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-09 23:12:25 +03:00
21a41a8e03 fix message body parsing
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-09 22:55:01 +03:00
9150958044 fix x-www-form-urlencoded requests
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-04-03 11:55:22 +03:00
Renovate Bot
e358db44ca Update module github.com/unistack-org/micro/v3 to v3.3.10 2021-04-01 00:36:29 +00:00
5 changed files with 52 additions and 21 deletions

2
go.mod
View File

@@ -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.9
require github.com/unistack-org/micro/v3 v3.3.13

7
go.sum
View File

@@ -5,10 +5,11 @@ 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.9/go.mod h1:M3RGgKZX1OlTn0QB+3R1tlSoGwmEg6rb/Isr0lLYmjQ=
golang.org/x/net v0.0.0-20210326220855-61e056675ecf/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
github.com/unistack-org/micro/v3 v3.3.13 h1:y4bDDkbwnjgOckrhFkC6D/o42tr75X33UbrB+Ko0M68=
github.com/unistack-org/micro/v3 v3.3.13/go.mod h1:98hNcMXp/WyWJwLwCuwrhN1Jm7aCWaRNsMfRjK8Fq+Y=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/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=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

31
http.go
View File

@@ -11,6 +11,7 @@ import (
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/unistack-org/micro/v3/broker"
@@ -19,6 +20,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,14 +102,20 @@ 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 {
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())
}
}
}
if len(b) > 0 {
hreq.Body = ioutil.NopCloser(bytes.NewBuffer(b))
@@ -126,6 +134,9 @@ func (h *httpClient) call(ctx context.Context, addr string, req client.Request,
}
ct := req.ContentType()
if len(opts.ContentType) > 0 {
ct = opts.ContentType
}
// set timeout in nanoseconds
header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
@@ -136,10 +147,10 @@ func (h *httpClient) call(ctx context.Context, addr string, req client.Request,
var err error
// get codec
switch ct {
case "x-www-form-urlencoded":
cf, err = h.newCodec(DefaultContentType)
case "application/x-www-form-urlencoded":
cf, err = h.newCodec(strings.Split(DefaultContentType, ";")[0])
default:
cf, err = h.newCodec(ct)
cf, err = h.newCodec(strings.Split(ct, ";")[0])
}
if err != nil {
@@ -187,13 +198,17 @@ func (h *httpClient) stream(ctx context.Context, addr string, req client.Request
}
ct := req.ContentType()
if len(opts.ContentType) > 0 {
ct = opts.ContentType
}
// set timeout in nanoseconds
header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
// set the content type for the request
header.Set("Content-Type", ct)
// get codec
cf, err := h.newCodec(req.ContentType())
cf, err := h.newCodec(strings.Split(req.ContentType(), ";")[0])
if err != nil {
return nil, errors.InternalServerError("go.micro.client", err.Error())
}
@@ -545,7 +560,7 @@ func (h *httpClient) Publish(ctx context.Context, p client.Message, opts ...clie
md["Content-Type"] = p.ContentType()
md["Micro-Topic"] = p.Topic()
cf, err := h.newCodec(p.ContentType())
cf, err := h.newCodec(strings.Split(p.ContentType(), ";")[0])
if err != nil {
return errors.InternalServerError("go.micro.client", err.Error())
}

View File

@@ -15,7 +15,6 @@ type httpRequest struct {
func newHTTPRequest(service, method string, request interface{}, contentType string, opts ...client.RequestOption) client.Request {
options := client.NewRequestOptions(opts...)
if len(options.ContentType) == 0 {
options.ContentType = contentType
}

18
util.go
View File

@@ -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 {
@@ -156,16 +160,24 @@ func newTemplate(path string) (util.Template, error) {
}
func parseRsp(ctx context.Context, hrsp *http.Response, cf codec.Codec, rsp interface{}, opts client.CallOptions) error {
// fast path return
if hrsp.StatusCode == http.StatusNoContent {
return nil
}
b, err := ioutil.ReadAll(hrsp.Body)
if err != nil {
return errors.InternalServerError("go.micro.client", err.Error())
}
if hrsp.StatusCode < 400 {
// unmarshal only if body not nil
if len(b) > 0 {
// unmarshal
if err := cf.Unmarshal(b, rsp); err != nil {
return errors.InternalServerError("go.micro.client", err.Error())
}
}
return nil
}
@@ -183,8 +195,12 @@ func parseRsp(ctx context.Context, hrsp *http.Response, cf codec.Codec, rsp inte
return errors.New("go.micro.client", string(b), int32(hrsp.StatusCode))
}
if len(b) > 0 {
if cerr := cf.Unmarshal(b, err); cerr != nil {
return errors.InternalServerError("go.micro.client", cerr.Error())
err = errors.InternalServerError("go.micro.client", cerr.Error())
}
} else {
err = errors.New("go.micro.client", string(b), int32(hrsp.StatusCode))
}
return err