woltapp/mitmproxy-mock

Question: How to mock server response from an URL?

KajeArch opened this issue · 2 comments

I doubt everyone will read this and I don't know if this belongs here, but I need a bit of an advice and I'm having quite a hard time figuring this out as JS seems a bit painful to me. Say, we send a typical GET request to an URL, like https://www.example.com/ with some string (doesn't matter what it is) and we receive the respond from the real server: "example string". How could I use mitmproxy-mock to now "catch" this certain HTTP response and, say, change the said string to a different string, like "oh that's my own!" ?
@arkku
@0neel

0neel commented

Do you want to alter the original response or just like to replace the content with something else?

arkku commented

It's a bit hard to answer when the example is so vague... e.g., do you only want to do this when the request matches a certain string or path? Is the example string part of some JSON structure, or do you want to just replace it everywhere?

Still, this configuration will replace the example string anywhere in all requests:

{
  "response":{
    "~":{
      "modify":[
        { "replace": [ "example string", "oh, that's my own!" ] }
      ]
    }
  }
}

To read that out in English: "in responses from backend, where the path matches ~ anything (nothing after ~), modify the response and replace occurrences of the regular expression example string with oh, that's my own!." This is a configuration format specific to Moxy. This case is quite close to the example https://github.com/woltapp/mitmproxy-mock/blob/master/config/rewrite-urls.json

To only do that replacement in certain paths, replace the "~" with either the exact path, or add a regular expression after the ~.