Search code examples
fiddler

Blocking a response whose body matches some condition


In Fiddler, is there a way to block a response if its body contains a particular word?

If Fiddler is capable of this (possibly through FiddlerScript?), that'd be awesome. Otherwise, if there is another tool that would be better, I'd love to hear about it.

A similar question was asked at What is the best way to block specific URL for testing?, but in my case I don't want to block a URI entirely, but rather only block certain responses from that URI, so that answer is not applicable.

Possible Leads

In FiddlerScript, there appears to be a function called utilFindInResponse, which might be incorporated into OnBeforeResponse like this:

static function OnBeforeResponse(oSession: Session) {
    ...
    if (oSession.utilFindInResponse("WordToBlock", false) > -1){
    oSession.responseCode = "404";
    }
}

Is this the right way to go about a response-blocker that searches for a particular word?


Solution

  • Yes, you're on the right track, but you should probably ensure that the response in question is HTML (or whatever text format you're expecting) before trying to search it.

    static function OnBeforeResponse(oSession: Session) {
      if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "text/"))
      {
        oSession.utilDecodeResponse();
        if (oSession.utilFindInResponse("WordToBlock", false) > -1)
        {
          oSession.responseCode = 404;
          oSession.utilSetResponseBody("blocked");
        }
      }
    }
    

    Also, keep in mind that the Streaming option (see Fiddler's toolbar) must be disabled for this code to work as you expect.