What are the handy scripting features of the "Grab Kit" Fiddler?

Source: Internet
Author: User
Tags json min regular expression reset

Fiddler (Official document address)

Fiddler custom scripts enable very powerful content substitution, including meaningful modification requests and return content. Specific methods can refer to the official website documentation.

# Fiddlerusescript: First you need to download the Script Editor fiddler Scriptediter in Fiddler->rules->costomize rules (if you need a ladder, try it yourself)



Introduction: Grab Kit tool, debug interface in addition to the existing functionality, you can also use flexible scripts to handle some scenarios


**************************************************
* * Website Script API Sample tutorial: http://docs.telerik.com/fiddler/ Knowledgebase/fiddlerscript/modifyrequestorresponse
**fiddler Command-line tutorial: http://docs.telerik.com/fiddler/ Knowledgebase/quickexec
* Recommended a Chinese blog: http://www.cnblogs.com/mcho/p/3983066.html
**fiddlerscript api:http:// Fiddlerbook.com/fiddler/dev/scriptsamples.asp
**jscript.net Official website tutorial: https://msdn.microsoft.com/en-us/library/ 91td9cas (v=vs.80). aspx
**google online forum: Https://groups.google.com/forum/?fromgroups#%21forum/httpfiddler
* * *

#该文件在window File directory location: C:\Users\ "You_pc_name" \documents\fiddler2\scripts

Fiddlerscript Scenario Application (20170627):


++++ ++++++++++++++beforerequest:++++++++++++++++++++++++++++

1. "Set the agent network speed limit" Here the amount of the 1KB 50kb/s need delay 160ms 
//Bandwidth: Mbps Kbps (bitstream)  speed: kb/s MB/s (Byte stream)
//Modify the check mark "simulate Modem speeds "[Randint (1,50) analog network jitter]
static function Randint (min, max) {
    return Math.Round (Math.random () * ( max-min) +min);
}
if (m_simulatemodem) {
    //Delay sends by 300ms per KB uploaded.
    osession["Request-trickle-delay"] = "" +randint (1,50);
    Delay receives by 150ms per KB downloaded.
    osession["Response-trickle-delay"] = "" +randint (1,50);
}
2. Here "filter and highlight host"
if (oSession.host.IndexOf ("host") >-1 | | oSession.host.IndexOf ("host") >-1) {
	osession["Ui-color"] = "green";
}
3. Here "Filter URL and highlight"
if (oSession.url.IndexOf ("Url_path") >-1) {
    osession["ui-color"] = "yellow";
    }
4. The "redirect Urlplace" host and URL are determined here  
if (Osession.hostnameis ("host") && oSession.url.IndexOf ("Url_path") >-1) {
	osession.hostname = "Host"
	}


+++++++++++++++++++beforerespond:++++++++++++++++++++++++

Buffer processing needs to be set in the return header, otherwise the body cannot be modified in Onbeforeresponse (the modified action will not block the original return)
static function Onpeekatresponseheaders (osession:session) {
	if (Osession.hostnameis ("cmshow.qq.com") && OSession.oResponse.headers.ExistsAndContains ("Content-type", "text/html")) {
		osession.bbufferresponse = true;    
	}
}
//5. Modify the Bady content of response here "use regular match" if (Osession.hostnameis ("host") &&
	OSession.url.IndexOf ("Url_path") >-1) {//Get the body string var strbody=osession.getresponsebodyasstring () in response;
	Use the regular expression or replace method to modify the string var regx = ' Stream_mode ': \d*? '
	Strbody=strbody.replace (regx, ' Stream_mode ': 0 ');
	Play a dialog box to check the modified body Fiddlerobject.alert (strbody);
The modified body will be re-written back to request in Osession.utilsetresponsebody (strbody); }
//6. Modify the data in the JSON here "modify the value of the interface field" if (Osession.hostnameis ("host") && OSession.url.IndexOf ("Url_path") >-1) {//Get response body in JSON string var responsestringoriginal = Osession.getresponse
	Bodyasstring ();
	 Convert to an editable jsonobject variable var responsejson = Fiddler.WebFormats.JSON.JsonDecode (responsestringoriginal);  
	 Modify the Jsonobject variable, modify the field data responsejson.jsonobject["New_core"] = "True";
	 responsejson.jsonobject["Stream_mode"] = 5;
	 Reset Response Body var responsestringdestinal = Fiddler.WebFormats.JSON.JsonEncode (Responsejson.jsonobject);
	Osession.utilsetresponsebody (responsestringdestinal); }
//7. Modify the data in the JSON here to "increase interface field = value" if (Osession.hostnameis ("host") && OSession.url.IndexOf ("Url_path") >-1) {//Get response body in JSON string var responsestringoriginal = Osession.getresponse
	Bodyasstring ();
	Convert to an editable jsonobject variable var responsejson = Fiddler.WebFormats.JSON.JsonDecode (responsestringoriginal);
	Modify the Jsonobject variable, modify the field data responsejson.jsonobject["Type_arr"] = ["Bullet"];
	Reset Response Body var responsestringdestinal = Fiddler.WebFormats.JSON.JsonEncode (Responsejson.jsonobject);
Osession.utilsetresponsebody (responsestringdestinal); }
//8. To enable the specified URL to support cors cross-domain requests//Sometimes, you call a JSON interface, discover cross-domain, you need to find the interface of the developer support cross-domain, obviously silly waiting for the backend development finished re-linking is inefficient,// This time will be in the background before the completion of their own cross-domain simulation, at this time fiddler is clearly a good weapon.  Branch to hold the Cors cross-domain,//is to increase the Access-control-allow-origin property for the requested return header, so you need to modify the Onbeforeresponse function to add your CORS logical static function at the end of the function Onbeforeresponse (osession:session) {... if (osession.uricontains ("URL to Process")) {osession.oresponse["
		Access-control-allow-origin "] =" allowed domain name ";
	osession.oresponse["Access-control-allow-credentials"] = true; }
}
//9. Different ports or directories of the same domain are forwarded to different servers//In some cases, a domain name deploys multiple business applications, but sometimes you just need to modify your application, and this time you will use the hosts to point the domain to the development machine,/ /But the problem is that all applications under that domain point to the same development machine, so that other applications still point to the formal environment. Obviously relying on the traditional hosts tool does not solve this problem,//Then you need to write fiddler rule script: static function Onbeforeresponse (osession:session) {... if (
	Osession.host = = "Www.google.com:80") {osession["x-overridehost"] = "123.123.123.123";
	} if (OSession.pathAndQuery.contains ("/path1/") {osession["x-overridehost"] = "124.124.124.124";
	}else if (oSession.pathAndQuery.contains ("/path2/") {osession["x-overridehost"] = "125.125.125.125"; }
}
10. Scene-The interface is also returned to change to 404, this way can solve the BPU command of the shortcomings of the individual debugging; All breakpoints require too many steps to operate, wasting time
if (Osession.hostnameis ("host1") && OSession.url.IndexOf ("Url_path1") >-1) {
	//indicates that the playback request interface has been received, and its return network status code is modified to: 404
	OSession.oResponse.headers.HTTPResponseCode = 404;
	OSession.oResponse.headers.HTTPResponseStatus = "Use fiddler change responed Code";
	
	}
if (Osession.hostnameis ("Host2") && oSession.url.IndexOf ("Url_path2") >-1) {
	//Ibid
	. OSession.oResponse.headers.HTTPResponseCode = 404;
	OSession.oResponse.headers.HTTPResponseStatus = "Use fiddler change responed Code";
	}


Summarize:
Fiddler and Charles used the scene, is a matter of opinion, see a hobby, like to make a small script, you can choose Fiddler, and finally what good scene script to continue to update.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.