Skip to content

Instantly share code, notes, and snippets.

@SabinT
Created September 21, 2018 22:31
Show Gist options
  • Save SabinT/82cf9d1a3b34755289899d79bbd39af2 to your computer and use it in GitHub Desktop.
Save SabinT/82cf9d1a3b34755289899d79bbd39af2 to your computer and use it in GitHub Desktop.
Replace JSON properties in HTTP response using Fiddler and JSON.NET
// Make sure to add references to Newtonsoft.Json.dll and System.Core.dll into Tools\Options\Scripting\References
// e.g., C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Core.dll;C:\Users\username\AppData\Local\Programs\Fiddler\Newtonsoft.Json.dll
public static void OnBeforeResponse(Session oSession)
{
if (m_Hide304s && oSession.responseCode == 304)
{
oSession["ui-hide"] = "true";
}
if (oSession.oRequest.host == "www.myhost.com" &&
oSession.url.Contains("/My/Api")) {
// FiddlerObject.log("Api matched!");
oSession.utilDecodeResponse();
var responseString = System.Text.Encoding.UTF8.GetString(oSession.ResponseBody);
JObject parsed = JObject.Parse(responseString);
// Replace the value of tokens named "token" at all levels.
foreach (JToken token in parsed.SelectTokens("$..tokens"))
{
JProperty jProperty = token.Parent as JProperty;
if (jProperty != null)
{
jProperty.Value = "changed";
}
}
string transformed = parsed.ToString();
oSession.ResponseBody = System.Text.Encoding.UTF8.GetBytes(transformed);
// FiddlerObject.log(transformed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment