httptoolkit / httptoolkit-server

The backend of HTTP Toolkit

Home Page:https://httptoolkit.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to configure mock requests without any browser interaction.

vikasg603 opened this issue · comments

Hey HTTP toolkit team, currently I am trying to set up some automation where I am interested to mock some requests and send that to my local server for some processing.

I can easily do this when I am on GUI but not able to figure it out the same when not using any browser but depends entirely on the terminal.

I also tried login my HTTP toolkit pro account but even this is not possible since your login server is checking the cookie and there's no way, I can provide any configurations to the HTTP toolkit server for things related to these.

Is there any way, i can configure login and mock requests using some JSON files?

If you're trying to configure a totally headless HTTP proxy, I think you probably don't want to use this repo, you want to use Mockttp directly. That's the internals of HTTP Toolkit's HTTP proxy itself, as a standalone open-source library.

You can use that to define rules in code to mock responses or redirect traffic, with no UI required, and in real code so you can implement any kind of logic you like. There's a getting started guide here: https://httptoolkit.tech/blog/javascript-mitm-proxy-mockttp/.

Does that work for your use case? If not, can you explain a bit more about what you're trying to do?

@pimterry definitely I was looking for something similar but this again creates some issues like how can I connect Mockttp to HTTP toolkit android application.
With HTTP toolkit android app we can avoid installing system certificate and that's make our work easy.

Ah ok, I see. I think Mockttp is still the way to go but you'll need some extra code to handle the Android setup, yes.

Can you share some more about what you're trying to do? What application are you intercepting and why, on what kinds of devices?

I can't name the application but can explain what we are trying to achieve.
Let's say the app name is X.
In X when we create an account there we have some captcha verification and we are trying to automate the whole process using genymotion cloud + ubuntu.
When I am using the HTTP toolkit locally I can redirect the captcha page to some local server where it will solve the captcha using 2captcha API and then will return a custom response, Which X will assume as a valid response and will allow me to register an account.

But the problem which I am facing currently

  1. HTTP toolkit pro is using the session to identify user login so need browser interaction for anything related to login.
  2. If using Mockttp, the android setup will be completely manual, which I want to avoid.

I definitely think you don't want to use the HTTP Toolkit UI in this case - that's entirely designed for manual debugging etc, not automation. Even if you get it working now, it will definitely change in future in frequent and unpredictable ways that will break your setup.

I think you probably want to:

  • Launch httptoolkit-server (installable via npm) by itself.
  • Use Mockttp with Mockttp.getRemote({ standaloneServerUrl: 'http://127.0.0.1:45456' }) to externally start a Mockttp proxy through HTTP Toolkit server.
  • Use that Mockttp proxy to define your custom rules.
  • Call the HTTP Toolkit Server GraphQL API manually to run Android ADB interception:
    activateInterceptor(
    id: ID!,
    proxyPort: Int!,
    options: Json
    ): Json
    (look at the network traffic inside HTTP Toolkit itself via View -> Toggle developer tools for an example).

That should work, and work reliably because you'd have fixed dependencies through npm.

A couple of caveats:

  • The GraphQL API here is internal so this kind of thing isn't officially supported, and it could change in theory with future updates. In practice it's fairly likely to stay compatible though, since breaking changes here would be inconvenient for HTTP Toolkit too.
  • While Mockttp is very liberally licensed (MIT), the HTTP Toolkit server (and other HTTP Toolkit-specific components) are only licensed as AGPL. That basically means if you let anybody use anything based on this code (either directly or as a network service) then you have to open-source all the related code. More info here: https://fossa.com/blog/open-source-software-licenses-101-agpl-license/ and https://tldrlegal.com/license/gnu-affero-general-public-license-v3-(agpl-3.0).

Hey, @pimterry thanks for the guide, I will definitely go through it, and about connecting ADB with graphql, I was actually having an API which I got using the same method you mentioned.
I was only having an issue with connecting the HTTP toolkit with Mockttp, but with your guide, it will be easy for me to configure this.
About the licensing part, I would love to make my code open source so that newcomers can understand it easily.