question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What about `before_request` hook?

See original GitHub issue

Didn’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:open
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
joaoecommented, Aug 15, 2022

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.

1reaction
sigmavirus24commented, Sep 15, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found