You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Firstly thank you for creating such a great tool! 😄
I find it's inconvenient for passing user custom parameters when using user-defined middleware. For example, in our development process, the server may receive an X-Request-ID header and it should pass this header to any services it calls. That's a simple way for tracing RPC calling actually. Besides of this, we also have some requirements for generating signature per request.
For current version of resty, the only way to approach this is using SetPreRequestHook and carrying user values with req.RawRequest.Context(), e.g:
client.SetPreRequestHook(func(c *resty.Client, req *resty.Request) error {
requestId, ok := req.RawRequest.Context().Value(requestIdKey).(string)
if ok && requestId != "" {
req.SetHeader("x-request-id", requestId)
}
authKey, ok := req.RawRequest.Context().Value(authKey).(string)
if !ok {
return errors.New("no auth key in request context")
}
// ... grab request info to generate signature...
req.SetHeader("x-signature", "...")
return nil
})
When using this client, we need a nested context to store these values:
We can only use SetPreRequestHook rather than OnBeforeRequest because RawRequest is created after all OnBeforeRequest middlewares and the context of RawRequest if the only place that can carry these values. But there are some shortcoming:
The headers added in SetPreRequestHook cannot be printed when debug is on since requestLogger happens before this hook
It's not so good to create so many context
So here is my modest proposal. A GetContext() method can be added into Request so we can put these codes in OnBeforeRequest and make request logger prints completed headers. Furthermore, the Request may contain a map[string]interface{} registry to store any kind of user values to get more abilities.
The text was updated successfully, but these errors were encountered:
@clippit Thanks for bringing up your concerns and your appreciations. I understand your suggestions.
I will bring following updates:
Expose context via Request.Context()
Move request logger after PreRequestHook call
jeevatkm
changed the title
Proposal: Add GetContext() method or other ways for accessing user value in hooks
Proposal: Add Context() method into Request and consider logging prerequesthook updates
Jan 4, 2019
Hi,
Firstly thank you for creating such a great tool! 😄
I find it's inconvenient for passing user custom parameters when using user-defined middleware. For example, in our development process, the server may receive an
X-Request-ID
header and it should pass this header to any services it calls. That's a simple way for tracing RPC calling actually. Besides of this, we also have some requirements for generating signature per request.For current version of resty, the only way to approach this is using
SetPreRequestHook
and carrying user values withreq.RawRequest.Context()
, e.g:When using this client, we need a nested context to store these values:
We can only use
SetPreRequestHook
rather thanOnBeforeRequest
becauseRawRequest
is created after allOnBeforeRequest
middlewares and the context of RawRequest if the only place that can carry these values. But there are some shortcoming:SetPreRequestHook
cannot be printed when debug is on sincerequestLogger
happens before this hookSo here is my modest proposal. A
GetContext()
method can be added intoRequest
so we can put these codes inOnBeforeRequest
and make request logger prints completed headers. Furthermore, theRequest
may contain amap[string]interface{}
registry to store any kind of user values to get more abilities.The text was updated successfully, but these errors were encountered: