fixup header filling after making new request

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-07-09 12:25:26 +03:00
parent 59d6c26003
commit 520dc29f89

23
http.go
View File

@ -123,23 +123,11 @@ func newRequest(addr string, req client.Request, ct string, cf codec.Codec, msg
} }
func (h *httpClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error { func (h *httpClient) call(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
header := make(http.Header, 2)
if md, ok := metadata.FromOutgoingContext(ctx); ok {
for k, v := range md {
header.Set(k, v)
}
}
ct := req.ContentType() ct := req.ContentType()
if len(opts.ContentType) > 0 { if len(opts.ContentType) > 0 {
ct = opts.ContentType 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)
cf, err := h.newCodec(ct) cf, err := h.newCodec(ct)
if err != nil { if err != nil {
return errors.InternalServerError("go.micro.client", err.Error()) return errors.InternalServerError("go.micro.client", err.Error())
@ -149,7 +137,16 @@ func (h *httpClient) call(ctx context.Context, addr string, req client.Request,
return err return err
} }
hreq.Header = header if md, ok := metadata.FromOutgoingContext(ctx); ok {
for k, v := range md {
hreq.Header.Set(k, v)
}
}
// set timeout in nanoseconds
hreq.Header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
// set the content type for the request
hreq.Header.Set("Content-Type", ct)
// make the request // make the request
hrsp, err := h.httpcli.Do(hreq.WithContext(ctx)) hrsp, err := h.httpcli.Do(hreq.WithContext(ctx))