This is a migrated thread and some comments may be shown as answers.

FiddlerScript: replace image in HTTP 200 response programmatically

2 Answers 806 Views
Fiddler Classic
This is a migrated thread and some comments may be shown as answers.
john
Top achievements
Rank 1
john asked on 31 Dec 2014, 05:38 PM
So I want to modify a standard Http 200 response from a server, which contains headers and an image in the responsebody. I want to selectively replace that image (ideally from  a stored image file; but the actual image will change over time and/or I want to select different images based on logic). I want to retain the original headers from the server response, and replace the responsebody image. I would use the OnBeforeResponse event to trap the response. The images are standard files (image file only, no http headers).

I see I can load an entire response from a file with FildderScript, but I can't see how to load just the responsebody from a file (binary image jpg/or png or gif). I thought possibly I could populate an array from the image file, and then use ResponseBody:System.Byte[] call to attach the image (and fix up the headers associated with the size/content type etc).

I have searched far and wide but haven't found any related example FiddlerScript for this operation. Could you post a snippet along these lines?

2 Answers, 1 is accepted

Sort by
0
Eric Lawrence
Telerik team
answered on 02 Jan 2015, 05:02 PM
If you want the client to see your changes, you must ensure that the specific session has streaming disabled (e.g. oSession.bBufferResponse = true); you can set this inside OnBeforeRequest or OnPeekAtResponseHeaders.

Replacing the body is trivial:

   oSession.ResponseBody = File.ReadAllBytes("C:\\temp\\myimage.jpg");

The ResponseBody property setter will automatically correct the Content-Length and encoding related headers. If you change the MIME type (e.g. returning a JPG rather than a GIF) you may manually need to update the Content-Type header:

      oSession.oResponse["Content-Type"] = "image/jpeg";

To use the File.ReadAllBytes method, you will need to ensure that you have

   import System.IO

at the top of your script, and that the file path you use has all \ characters escaped to \\.
 

Regards,
Eric Lawrence
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
john
Top achievements
Rank 1
answered on 03 Jan 2015, 06:46 PM
Eric,

Thanks very much for the fast and thorough response; that got me straightened out.
Tags
Fiddler Classic
Asked by
john
Top achievements
Rank 1
Answers by
Eric Lawrence
Telerik team
john
Top achievements
Rank 1
Share this question
or