Intercepting (and modifying) outgoing HttpPoson requests

Before I start, I know there is a similar question, but I could not find an adequate answer there.

I’m working on a service that interacts with GitHub using Tentacat library (which then uses HttpPoison to send requests to GitHub servers). I would like to intercept all of those HttpPoison requests before they “leave my service” to add an extra header. I know similar libraries (Axios for React) have this feature of interceptors. Is something like that even possible with HttpPosion?

Thanks in advance.

1 Like

Nothing that I am aware of but maybe you can use Tesla with the Hackney adapter below? That allows you to add a middleware that changes the headers?

Granted I don’t know if your requirements are written in stone (f.ex. Tentacat) but if not, your scenario can be served.

1 Like

Yup, the entire codebase is already written using Tentacat, so changing that is not an option. Thanks for the reply

If it is not possible to alter codebase then you need to create proxy service that will intercept and alter all the requests coming through

2 Likes

That was the option I was trying to avoid :smiley: Maybe I wasn’t specific enough in my last response, I can alter the codebase, but it’s not feasible to replace Tentacat as it is being used all over the place and it would take too much time to create my own GitHub client and switch to it

In that case your best option is to fork Tentacat and modify it. That should be relatively easy to do. Maintenance long-term however… yeah, not good.

Or maybe post a PR offering such functionality that its users can opt-in to?

1 Like

This config option documented in the Misc section of the README seems to do what you want:

config :tentacat, :extra_headers, [{"Accept", "application/vnd.github.black-cat-preview+json"}]
3 Likes

Actually looking through here – GitHub - edgurgel/tentacat: Simple Elixir wrapper for the GitHub API – I am seeing a possibility to add extra headers?

EDIT: @sneako beat me by 10 seconds. :smiley:

3 Likes

@sneako @dimitarvp Thanks, I missed that partt

1 Like

@sneako @dimitarvp Actually, that allows me only to set headers statically, I can’t use it to add Etag (If-None-Match) which differs for every repository

Looked quickly through tentacat. It seems your only option is to fork it and modify lib/tentacat.ex to do use HTTPoison.Base but with process_request_headers overriden in the same file (lib/tentacat.ex).

That’s the quickest fix I can think of. If not then you should bring a tank to the fist fight and setup a man-in-the-middle proxy to intercept and inject headers.

2 Likes