This is a migrated thread and some comments may be shown as answers.

Fiddler change API server response?

1 Answer 1682 Views
Fiddler Classic
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 09 Mar 2020, 02:11 AM

Hello, I am trying to use FiddlerScript to change a json response from my API server to my client. I keep on getting an error when I have my client connect to the server. I am new to FiddlerScript so I don't know if I am doing anything wrong.

This is the code I am using in Fiddler ScriptEditor:

01.// Here is code to modify server's response
02.if(oSession.host == ("api.server") && oSession.uriContains("/config/url/1")) {
03.    // Color this response, so we can spot it in Fiddler
04.    oSession["ui-backcolor"] = "lime";
05. 
06.    // Convert the request body into a string
07.    var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
08. 
09.    // Convert the text into a JSON object
10.    var j1 = Fiddler.WebFormats.JSON.JsonDecode(oBody);
11. 
12.    //Change the value
13.    j1.JSONObject["data1"]["data2"]["data3"]["data4"]["Value"] = "1000";
14.    j1.JSONObject["data1"]["data2"]["data3"]["Value"] = "1000";
15. 
16.    // Convert back to a byte array
17.    var modBytes1 = Fiddler.WebFormats.JSON.JsonEncode(j1.JSONObject);
18. 
19.    // Convert json to bytes, storing the bytes in request body
20.    var mod = System.Text.Encoding.UTF8.GetBytes(modBytes1);
21.    oSession.RequestBody = mod;
22.}
23. 
24.// Here is code to modify server's response
25.if(oSession.host == ("api.server") && oSession.uriContains("/config/url/2")) {
26.    // Color this response, so we can spot it in Fiddler
27.    oSession["ui-backcolor"] = "lime";
28. 
29.    // Convert the request body into a string
30.    var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
31. 
32.    // Convert the text into a JSON object
33.    var j2 = Fiddler.WebFormats.JSON.JsonDecode(oBody);
34. 
35.    //Change the value
36.    j2.JSONObject["data1"]["data2"]["data3"]["data4"]["Value"] = "1000";
37.    j2.JSONObject["data1"]["data2"]["data3"]["Value"] = "1000";
38. 
39.    // Convert back to a byte array
40.    var modBytes2 = Fiddler.WebFormats.JSON.JsonEncode(j2.JSONObject);
41. 
42.    // Convert json to bytes, storing the bytes in request body
43.    var mod = System.Text.Encoding.UTF8.GetBytes(modBytes2);
44.    oSession.RequestBody = mod;
45.}
46. 
47.// Here is code to modify server's response
48.if(oSession.host == ("api.server") && oSession.uriContains("/config/database")) {
49.    // Color this response, so we can spot it in Fiddler
50.    oSession["ui-backcolor"] = "lime";
51.    FiddlerApplication.Log.LogFormat(oBody);
52. 
53.    // Convert the request body into a string
54.    var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
55. 
56.    // Convert the text into a JSON object
57.    var j3 = Fiddler.WebFormats.JSON.JsonDecode(oBody);
58. 
59.    //Change the value
60.    j3.JSONObject["Value"] = "1";
61.    j3.JSONObject["data1"]["Value"] = "1000";
62. 
63.    // Convert back to a byte array
64.    var modBytes3 = Fiddler.WebFormats.JSON.JsonEncode(j3.JSONObject);
65. 
66.    // Convert json to bytes, storing the bytes in request body
67.    var mod = System.Text.Encoding.UTF8.GetBytes(modBytes3);
68.    oSession.RequestBody = mod;
69.}
70. 
71.// Here is code to modify server's response
72.if(oSession.host == ("api.server") && oSession.uriContains("/config")) {
73.    // Color this response, so we can spot it in Fiddler
74.    oSession["ui-backcolor"] = "lime";
75. 
76.    // Convert the request body into a string
77.    var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
78. 
79.    // Convert the text into a JSON object
80.    var j4 = Fiddler.WebFormats.JSON.JsonDecode(oBody);
81. 
82.    //Change the value
83.    j4.JSONObject["Value"] = "1";
84.    j4.JSONObject["data1"]["Value"] = "1000";
85. 
86.    // Convert back to a byte array
87.    var modBytes4 = Fiddler.WebFormats.JSON.JsonEncode(j4.JSONObject);
88. 
89.    // Convert json to bytes, storing the bytes in request body
90.    var mod = System.Text.Encoding.UTF8.GetBytes(modBytes4);
91.    oSession.RequestBody = mod;
92.}

I have attached screenshots of the setup and the error codes. 

1 Answer, 1 is accepted

Sort by
0
Eric R | Senior Technical Support Engineer
Telerik team
answered on 10 Mar 2020, 04:04 PM

Hi Ryan,

Thank you for providing the resources. Although, the only issue I can see is that the JSON Array is null before converting it back to a ByteArray or during Encoding. Fiddler does provide a logging API that makes it possible to log the output. For example, in the below code snippet, the response body is logged as a string.

FiddlerApplication.Log.LogString(oSession.GetResponseBodyAsString())

Additionally, I recommend reviewing the Understanding FiddlerScript blog post for an overview of how it works. For more examples, see the Modifying a Request or Response article as well.

Please give this a try let me know if you need any additional information. Thank you for using the Fiddler Forums.

Regards,


Eric R | Senior Technical Support Engineer
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Fiddler Classic
Asked by
Ryan
Top achievements
Rank 1
Answers by
Eric R | Senior Technical Support Engineer
Telerik team
Share this question
or