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.