FiddlerScript: replace image in HTTP 200 response programmatically

3 posts, 0 answers
  1. john
    john avatar
    2 posts
    Member since:
    Dec 2014

    Posted 31 Dec 2014 Link to this post

    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. Eric Lawrence
    Eric Lawrence avatar
    833 posts

    Posted 02 Jan 2015 Link to this post

    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 \\.

    Eric Lawrence

    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.

  3. john
    john avatar
    2 posts
    Member since:
    Dec 2014

    Posted 03 Jan 2015 in reply to Eric Lawrence Link to this post


    Thanks very much for the fast and thorough response; that got me straightened out.
Back to Top