go-resty / resty

Simple HTTP and REST client library for Go
MIT License
9.68k stars 681 forks source link

Signing requests with path parameters #771

Closed lfrestrepog closed 4 months ago

lfrestrepog commented 5 months ago

Hi, good day!

Some endpoints of an API I'm using require a signature of the request, as follows:

  1. Concatenate a nonce, the method, the url and the params, separated with |. So nonce|method|url|params
  2. Sign that string using hmac sha256 and my key.

All good, except when I use path parameters the url should have the actual parameters instead of the placeholders. As a workarround I just build my path string with fmt.Sprintf instead of using the path parameters feature. It would be better if there was any way to access the final path before the request is sent to the server, perhaps a callback like OnBeforeRequest. I actually tried it but, as is well described in the documentation, it is called before resty's middlewares, so the path parameters are not yet replaced.

What's the recommended way to sign requests when one needs the path to be signed too?

jeevatkm commented 4 months ago

@lfrestrepog You could use SetPreRequestHook to achieve it.