FiddlerScript script uses regular expressions to replace response content

When using Fiddler to capture packets, partial matching and replacement of the response content of a specific url interface can be done by writing code in the OnBeforeResponse event of FiddlerScript.

The following code example in this article is to replace specific content in an interface request, as follows:


    static function OnBeforeResponse(oSession: Session) {
    
    
        if (m_Hide304s && oSession.responseCode == 304) {
    
    
            oSession["ui-hide"] = "true";
        }
        
        // 判断为指定URL接口
        if (oSession.url.IndexOf("/api/detail.json")>-1) {
    
    
            //decode响应体
            oSession.utilDecodeResponse();
            
            //普通字符串替换,不支持正则表达式
            //oSession.utilReplaceInResponse("buttonType","buttonType111");
            
            //下面是使用正则表达式替换(获取Body后改写Body内容在通过代码设置到响应中)
            var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
            //判断响应体中存在所需内容
            if(oBody.IndexOf('"btnType":"05"')>-1){
    
    
                //使用正则表达式替换目标内容
                var oRegEx = /"btnType":"\d+"/gi;
                oBody = oBody.replace(oRegEx, '"btnType":"01"');
                //将处理后的Body设置到响应中
                oSession.utilSetResponseBody(oBody); 
            }
        }
    }

More reference about Fiddler's script processing: https://www.kancloud.cn/cyyspring/test/935188


(END)

Guess you like

Origin blog.csdn.net/catoop/article/details/132900368