What about `before_request` hook?
See original GitHub issueDidn’t find a correct place where to post my question.
Hook system is a cool idea but I’m wondered why there is no some kind of ‘before_request’ hook?
Let’s assume I want to create requests.Session
, and do some preparation steps for upcoming requests (like body formatting or simply log what URL and method). One of possible ways to do that is to override requests.Session.request
method, but adding one more hook sounds more reasonable. It is possible to put it here https://github.com/requests/requests/blob/master/requests/sessions.py#L623
Is there any reason of not having this hook? Or is there some appropriate way to execute some piece of code before each request within Session?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:7 (3 by maintainers)
Top Results From Across the Web
got/9-hooks.md at main · sindresorhus/got - GitHub
To modify it, use a beforeRequest hook instead. Note: This hook is called when a new instance of Options is created. Do not...
Read more >Intercept each request with before and after hooks
I have to make 5 requests (order doesn't matter) to 5 different endpoints. The URL of these endpoints is the same, except for...
Read more >Request hooks - Draqula
These hooks let you intercept request and response stage of the query or mutation and modify basically everything. beforeRequest. This hook allows you...
Read more >Replacing body/form/json in beforeRequest hook ... - Issuehunt
Editing options.body inside the beforeRequest hook does not affect it. Node.js version: v14.4.0; OS & version: Windows 10 ...
Read more >Ky | Best of JS
Hooks allow modifications during the request lifecycle. Hook functions may be async and are run serially. hooks.beforeRequest. Type: Function[] Default: [].
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Howdy.
If you support the concept of hooks in the requets API, they should be comprehensive and allow a lot of control over how the API works. So far only a “response” hook is partially useful.
My use case: I want to run the API in either capturing mode (write all responses to disk, for that the response hook works) or read back mode (for this case I’d need a “request” hook before the request is sent to produce a response. And/or, a global hook when a session is created so I can override the HTTPAdapters to an adapter that reads back from the cache.
Thank you.
This could also be useful for adding distributed tracing support to requests. I had thought about using a TransportAdapter for that though, so really
before_request
isn’t entirely necessary