Created
September 21, 2018 22:31
-
-
Save SabinT/82cf9d1a3b34755289899d79bbd39af2 to your computer and use it in GitHub Desktop.
Replace JSON properties in HTTP response using Fiddler and JSON.NET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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