From d9fef28c7800369a1f2c10a7d6d87cf11360a031 Mon Sep 17 00:00:00 2001 From: roc Date: Wed, 28 Jun 2023 10:26:34 +0800 Subject: [PATCH] make sure beforeRequests executed before client middleware(fix #248) --- client.go | 15 --------------- request.go | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/client.go b/client.go index f95fe32f..b6316b86 100644 --- a/client.go +++ b/client.go @@ -1516,21 +1516,6 @@ func (c *Client) roundTrip(r *Request) (resp *Response, err error) { } }() - if r.Headers == nil { - r.Headers = make(http.Header) - } - - for _, f := range r.client.udBeforeRequest { - if err = f(r.client, r); err != nil { - return - } - } - for _, f := range r.client.beforeRequest { - if err = f(r.client, r); err != nil { - return - } - } - // setup trace if r.trace == nil && r.client.trace { r.trace = &clientTrace{} diff --git a/request.go b/request.go index 05ef2fc5..63df395d 100644 --- a/request.go +++ b/request.go @@ -587,6 +587,20 @@ func (r *Request) do() (resp *Response, err error) { }() for { + if r.Headers == nil { + r.Headers = make(http.Header) + } + for _, f := range r.client.udBeforeRequest { + if err = f(r.client, r); err != nil { + return + } + } + for _, f := range r.client.beforeRequest { + if err = f(r.client, r); err != nil { + return + } + } + if r.client.wrappedRoundTrip != nil { resp, err = r.client.wrappedRoundTrip.RoundTrip(r) } else {