Fiddler腳本修改Response數據


Instroduction:

Fiddler抓包工具,修改CustomRules.js腳本達到修改Http請求的Response中Body信息(如JSON串)。

常用於在Server開發未完全Ready而前端或客戶端開發需要Server數據時,修改請求的返回數據,達到Debug和測試的目的,較添加BreakPoint的方法更加便捷。

Implementation:

    目的:

  本例Demo中會為JSON添加一個字段和修改一個字段,如下所示:

 

 1 // 原JSON串 V1.0
 2 {    
 3   "music": "big big world",
 4   "singer": "Emilia Rydberg"
 5 } 
 6 
 7 // ====>>>>>
 8 
 9 // 新JSON串 V1.1
10 {
11   "music": "big big world",
12   "singer": "艾密莉亞·懷得堡",      // 修改該字段(英文名改為中文名顯示)
13   "similar song": [            // 添加該字段(相似歌曲列表)
14     {
15       "music": "dying in the sun",
16       "singer": "The Cranberries"
17     },
18     {
19       "music": "seasons in sun",
20       "singer": "WestLife"
21     }
22   ]
23 }

 

  基本流程:

    

  基本步驟(簡單):

  打開並編輯Customize Rule文件,在方法 OnBeforeResponse 中插入修改代碼,重啟Fiddler重新加載Rule,運行。

插入代碼:

 1 static function OnBeforeResponse(oSession: Session) {
 2         if (m_Hide304s && oSession.responseCode == 304) {
 3             oSession["ui-hide"] = "true";
 4         }
 5         
 6         // 判斷是否為目標請求
 7         var isMusicRequest = false;
 8         if ((oSession.host == "m.baidu.com") &&                // host  
 9             oSession.fullUrl.Contains("suggest?ctl=his&action=list"))   // url
10         {
11             isMusicRequest = true;
12         }
13         // 修改返回JSON串
14         if (isMusicRequest)
15         {
16             // 1, 獲取Response Body中JSON字符串
17             var responseStringOriginal =  oSession.GetResponseBodyAsString();
18             //FiddlerObject.log(responseStringOriginal);    // 可在控制台中輸出Log
19            
20             // 2, 轉換為可編輯的JSONObject變量
21             var responseJSON = Fiddler.WebFormats.JSON.JsonDecode(responseStringOriginal);
22             
23             // 3, 修改JSONObject變量
24             // 3.1修改字段
25             responseJSON.JSONObject['singer'] = "艾密莉亞·懷得堡";
26             // 3.2添加字段
27             var similarSong1= '{' +
28                               '"music": "dying in the sun",'+
29                               '"singer": "The Cranberries"'+
30                               '}';
31             var similarSong2= '{' +
32                               '"music": "seasons in sun",'+
33                               '"singer": "WestLife"'+
34                               '}';
35             
36             var similarSong = '[' +
37                               similarSong1 +
38                               ',' +
39                               similarSong2 +
40                               ']';
41             responseJSON.JSONObject['similar song'] = Fiddler.WebFormats.JSON.JsonDecode(similarSong).JSONObject ;
42             
43             // 4, 重新設置Response Body
44             var responseStringDestinal = Fiddler.WebFormats.JSON.JsonEncode(responseJSON.JSONObject);
45             //FiddlerObject.log(responseStringDestinal);
46             oSession.utilSetResponseBody(responseStringDestinal);
47         }
48     }

 

  Customize Rule編輯方式:

  可以通過Fiddler菜單打開CustomRules.js文件然后通過第三方編輯器來編輯並保存,或者通過Fiddler ScriptEditor工具來編輯(推薦,右側有相關類使用參考)。

    

  OR

   

References:

  1,Fiddler Script基本介紹:

  http://www.cnblogs.com/TankXiao/archive/2012/04/25/2349049.html

  2,官方文檔參考:

  http://docs.telerik.com/fiddler/KnowledgeBase/FiddlerScript/ModifyRequestOrResponse

  3,使用中踩坑借鑒:

  http://stackoverflow.com/questions/23094692/can-i-modify-json-responses-using-fiddler-scripts

  http://www.telerik.com/forums/how-to-use-fiddler-webformats-json-jsondecode

  4,FiddlerScript Editor:

  http://www.telerik.com/download/fiddler/fiddlerscript-editor

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM