90 | 90 |
ctx, cancel := context.WithCancel(ctx)
|
91 | 91 |
defer cancel()
|
92 | 92 |
|
93 | |
// Vars used for client finalizer to ensure there are no nil values
|
94 | 93 |
var (
|
95 | |
req *http.Request = &http.Request{}
|
96 | |
resp *http.Response = &http.Response{}
|
|
94 |
resp *http.Response
|
97 | 95 |
err error
|
98 | 96 |
)
|
99 | 97 |
if c.finalizer != nil {
|
100 | 98 |
defer func() {
|
101 | |
ctx = context.WithValue(ctx, ContextKeyResponseHeaders, resp.Header)
|
102 | |
ctx = context.WithValue(ctx, ContextKeyResponseSize, resp.ContentLength)
|
103 | |
c.finalizer(ctx, resp.StatusCode, req)
|
|
99 |
if resp != nil {
|
|
100 |
ctx = context.WithValue(ctx, ContextKeyResponseHeaders, resp.Header)
|
|
101 |
ctx = context.WithValue(ctx, ContextKeyResponseSize, resp.ContentLength)
|
|
102 |
}
|
|
103 |
c.finalizer(ctx, err)
|
104 | 104 |
}()
|
105 | 105 |
}
|
106 | 106 |
|
107 | |
req, err = http.NewRequest(c.method, c.tgt.String(), nil)
|
|
107 |
req, err := http.NewRequest(c.method, c.tgt.String(), nil)
|
108 | 108 |
if err != nil {
|
109 | 109 |
return nil, err
|
110 | 110 |
}
|
|
141 | 141 |
|
142 | 142 |
// ClientFinalizerFunc can be used to perform work at the end of a client HTTP
|
143 | 143 |
// request, after the response is returned. The principal
|
144 | |
// intended use is for request logging. In addition to the response code
|
145 | |
// provided in the function signature, additional response parameters are
|
|
144 |
// intended use is for error logging. Additional response parameters are
|
146 | 145 |
// provided in the context under keys with the ContextKeyResponse prefix.
|
147 | |
type ClientFinalizerFunc func(ctx context.Context, code int, r *http.Request)
|
|
146 |
// Note: err may be nil. There maybe also no additional response parameters depending on
|
|
147 |
// when an error occurs.
|
|
148 |
type ClientFinalizerFunc func(ctx context.Context, err error)
|
148 | 149 |
|
149 | 150 |
// EncodeJSONRequest is an EncodeRequestFunc that serializes the request as a
|
150 | 151 |
// JSON object to the Request body. Many JSON-over-HTTP services can use it as
|