Fiddler to log requests to txt-file (Fiddlerscript)

3,377 views
Skip to first unread message

PMetsapuro

unread,
May 16, 2016, 5:08:05 AM5/16/16
to Fiddler
Hi!

Is there an easy way to log particular http-request into plain text file. I would need this for test automation, so that log generation would not need any human interaction.
 I have been trying to make Fiddler script to write file but it fails because multiple instances are trying to write simultaneously. I guess there is several threads running at the same time.
I have put SaveRequestBody("C:\\temp\\foobar88.txt"), into OnPeekAtResponseHeaders also that fails.

FiddlerObject.log("my messsage")   could work if there would be automatic way to save the log into a text file.  

BR PMetsapuro

Michael

unread,
May 16, 2016, 8:12:13 AM5/16/16
to Fiddler
Hi there!.

You can put your code inside the OnBeforeRequest method in FiddlerScript. I wrote a quick script that logs the request and request body to a file only when the hostname is www.webservicex.net, which means that you can create a kind of rule when a specific host or URI is logged into the file. This is the Script  I wrote :


        if(oSession.host == "www.webservicex.net"){
            oSession.utilDecodeRequest();   //Decoding HTTP request in case it's gzip
            //Saving full request object (Including HTTP headers)
            oSession.SaveRequest("C:\\temp\\request.txt",true);
            //Saving just body
            oSession.SaveRequestBody("C:\\temp\\requestBody.txt");
        }


Here is a screenshot. I hope it helps



Eric Lawrence

unread,
May 16, 2016, 5:11:31 PM5/16/16
to Fiddler
Yes, you're likely to encounter file contention issues if you try to write to a single file from multiple threads. You could use a Mutex object to ensure that only one writer is active, or you could use a concurrent data structure in memory (or just a StringBuilder protected by a mutex) and write that data out on exit.

If you want to use the Fiddler log (beware that it will contain other strings as well) you can have it save to a file by logging the "magic" string @log.export, like so:

FiddlerApplication.Log.LogString('@log.export "C:\\temp\\out.txt"');

Eric Lawrence

unread,
May 16, 2016, 5:13:48 PM5/16/16
to Fiddler
For what it's worth, typically what you'd want to do is just let Fiddler capture all requests, then use one of the Transcoders to export the captured requests to one or more of the formats; e.g. from the Fiddler Book:

var oSessions = FiddlerApplication.UI.GetAllSessions();
var oExportOptions = FiddlerObject.createDictionary();
oExportOptions.Add("Filename", "C:\\users\\ericlaw\\desktop\\out1.har");
oExportOptions.Add("MaxTextBodyLength", 1024);
oExportOptions.Add("MaxBinaryBodyLength", 16384);
FiddlerApplication.DoExport("HTTPArchive v1.2", oSessions, oExportOptions, null); 

Reply all
Reply to author
Forward
0 new messages