Common Scripting Examples
This article collects practical examples that use the Session object (oSession) inside the OnBeforeRequest and OnBeforeResponse script hooks to inspect and modify traffic. Use them as a starting point for your own scripts.
OnBeforeRequestruns before a request is sent to the server, so only the request (oSession.oRequest) exists yet.OnBeforeResponseruns after a response is received, so both the request and the response (oSession.oResponse) are available—but any change you make to the request at that point is no longer sent to the server, since the server has already replied.
These examples are adapted from the equivalent FiddlerScript examples for Fiddler Classic, translated to plain C# and to the
OnBeforeRequest/OnBeforeResponsehooks used by Fiddler Everywhere. Use the script editor's autocomplete (type.afteroSession) to explore the members available onSessionin your installed version.
Add a Request Header
void OnBeforeRequest(Session oSession)
{
// Runs when a client request is received, before it is sent to the server.
oSession.oRequest["NewHeaderName"] = "New header value";
}
Remove a Response Header
void OnBeforeResponse(Session oSession)
{
oSession.oResponse.headers.Remove("Set-Cookie");
}
Replace Text in an HTML Response
Decode the response so it is no longer compressed or chunked, then search and replace within the body:
void OnBeforeResponse(Session oSession)
{
// Runs when a server response is received, before it is returned to the client.
if (oSession.HostnameIs("example.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type", "text/html"))
{
oSession.utilDecodeResponse();
oSession.utilReplaceInResponse("<title>", "<u>");
oSession.utilReplaceInResponse("</title>", "</u>");
}
}
Change the Path of a Request on the Same Server
void OnBeforeRequest(Session oSession)
{
if (oSession.PathAndQuery == "/version1.css")
{
oSession.PathAndQuery = "/version2.css";
}
}
Redirect All Requests for a Host to a Different Host
void OnBeforeRequest(Session oSession)
{
if (oSession.HostnameIs("www.example.com"))
{
oSession.hostname = "test.example.com";
}
}
Redirect All Requests for a Host and Port to a Different Host and Port
void OnBeforeRequest(Session oSession)
{
if (oSession.host == "www.example.com:8080")
{
oSession.host = "test.example.com:9090";
}
}
Redirect a Host, Including HTTPS Tunnels
Changing hostname redirects plain HTTP traffic. To also redirect HTTPS traffic to the same host, the CONNECT tunnel itself must be retargeted:
void OnBeforeRequest(Session oSession)
{
if (oSession.HTTPMethodIs("CONNECT") && oSession.PathAndQuery == "www.example.com:443")
{
oSession.PathAndQuery = "beta.example.com:443";
}
if (oSession.HostnameIs("www.example.com"))
{
oSession.hostname = "beta.example.com";
}
}
Retarget a Host Without Changing the Host Header
This simulates an entry in the operating system's HOSTS file: the request is sent to a different IP address, but the Host header the server sees is unchanged.
void OnBeforeRequest(Session oSession)
{
if (oSession.HostnameIs("subdomain.example.com"))
{
oSession.bypassGateway = true; // Skip any configured upstream proxy for this request.
oSession["x-overrideHost"] = "128.123.133.123"; // DNS name or IP address of the target server.
}
}
Retarget a Single URL, Changing the Host Header
void OnBeforeRequest(Session oSession)
{
if (oSession.url == "www.example.com/live.js")
{
oSession.url = "dev.example.com/workinprogress.js";
}
}
Remove Cookies from Outgoing Requests
void OnBeforeRequest(Session oSession)
{
oSession.oRequest.headers.Remove("Cookie");
}
Flag Responses That Contain a Search Term
void OnBeforeResponse(Session oSession)
{
if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "text/html") && oSession.utilFindInResponse("searchfor", false) > -1)
{
// Add a custom header so the match is easy to spot in the Inspector.
oSession.oResponse["X-Contains-Searchfor"] = "true";
}
}
Remove All <div> Tags from an HTML Response
void OnBeforeResponse(Session oSession)
{
if (oSession.oResponse.headers.ExistsAndContains("Content-Type", "html"))
{
oSession.utilDecodeResponse();
var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
var oRegEx = new System.Text.RegularExpressions.Regex(
"<div[^>]*>(.*?)</div>",
System.Text.RegularExpressions.RegexOptions.Singleline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
oBody = oRegEx.Replace(oBody, "");
oSession.utilSetResponseBody(oBody);
}
}
Spoof the User-Agent Header
void OnBeforeRequest(Session oSession)
{
oSession.oRequest["User-Agent"] = "Googlebot/2.X (+http://www.googlebot.com/bot.html)";
}
Request Content in a Specific Language
void OnBeforeRequest(Session oSession)
{
oSession.oRequest["Accept-Language"] = "he";
}
Block Requests That Match a Condition
void OnBeforeRequest(Session oSession)
{
if (oSession.uriContains(".css"))
{
oSession.oRequest.FailSession(404, "Blocked", "Fiddler Everywhere blocked this CSS file.");
}
}