Cannot change HTTP Status with oSession.responseCode =

95 views
Skip to first unread message

Steven Scott

unread,
Aug 7, 2018, 5:26:49 PM8/7/18
to Fiddler
I have written a number of functions in fiddler as a static function that gets called to parse the URL line that Fiddler received.  We use this as an external API emulator for testing.  While everything works when calling our code from within the OnPeekAtResponseHeaders() or OnBeforeResponse(), the response code cannot be changed.

Within either of these functions (have called my function from both with no success) we can call:

    static function OnBeforeResponse(oSession: Session) {
        oSession = Emulator(oSession); // Call our code
        ...
    }

or

    static function OnPeekAtResponseHeaders(oSession: Session) {
        oSession = Emulator(oSession);
        ...
    }

In our code, we basically look at the URI called:

    if (oSession.uriContains("/rest/SomeAPICall") ) {
        // Decide what text file on the OS to return based on parameters or RequestBody contents
        ...
    } else if (oSession.uriContains("/rest/SomeAPICall2") ) {
        // Decide what text file on the OS to return based on parameters or RequestBody contents
        ...
    } else if (oSession.uriContains("/rest/SomeAPICall3") ) {
        // Decide what text file on the OS to return based on parameters or RequestBody contents
        ...
   }

The return data is set using the OS

    var ResponseFile = new ActiveXObject("Scripting.FileSystemObject");
    if (ResponseFile.FileExists(ReturnFileName)) {
        oSession["x-replywithfile"] = ReturnFileName;
    } else {
  // Error message returned as the ReturnBody was not populated and Response File not found
        oSession.utilSetResponseBody(ErrorMessage);
     }

Based on the decisions on the file, we set a variable (HttpStatus) that we try to set in the responseCode

    oSession.responseCode = HttpStatus;
    oSession.oResponse.headers.HTTPResponseCode = HttpStatus;
    oSession.oResponse.headers.HTTPResponseStatus = HttpStatus + ' ' + HttpStatusText;

And then ultimately return the oSession to the caller function:

    return oSession;

While the results of our text files are returned in the response, I cannot get the status code to change from 200.  I must be doing something wrong but cannot find what it is.

Steven Scott

unread,
Aug 8, 2018, 2:25:46 PM8/8/18
to Fiddler
I enabled some logging after my calls, and the log shows that I have set the responseCode, but I do not get my responseCode changes returned.

14:22:22:2428 Session 2: Response header peek shows status is 200
14:22:22:2428 OnPeekAtResponseHeaders: Session 2: Response header peek shows status is 500 after Emulator()
14:22:22:2428 OnBeforeResponse: Session 2: Response header peek shows status is 500 after Emulator()

Steven Scott

unread,
Aug 8, 2018, 5:09:33 PM8/8/18
to Fiddler
Finally tracked it down.  The problem is that I am often returning a file when returning an error using the 'oSession["x-replywithfile"]'.  However, this always makes the status an 200 OK, even if I try to change the status after the x-replywithfile setting.

oSession["x-replywithfile"] = ReturnFileName;
oSession.responseCode = 500;
This will still always return a 200. 

Changing to the following will work.
var FileContents = ReadFile(ReturnFileName);
oSession.utilSetResponseBody(FileContents);
oSession.responseCode = 500;

EricLaw

unread,
Aug 9, 2018, 2:20:17 PM8/9/18
to Fiddler
ReplyWithFile returns a HTTP/200 response with autogenerated headers for the selected file. That's not what you want.

You also shouldn't be using ActiveXObject("Scripting.FileSystemObject"); either; instead, use the .NET Native objects, e.g. File.ReadAllBytes

For instance, here's something you can run inside your OnBeforeRequest handler to reply with a local file:

if (oSession.uriContains("www.domain.com/file.js")) { oSession.utilCreateResponseAndBypassServer(); oSession.oResponse.headers.Add("Content-Type", "relevant/mime-type"); oSession.ResponseBody = System.IO.File.ReadAllBytes("C:\\Your\\File.txt"); }

If you instead want to wait for the server's response, remove utilCreateResponseAndBypassServer and instead just modify the response objects.

legend

unread,
Sep 9, 2018, 9:28:51 PM9/9/18
to Fiddler
oSession.utilSetResponseBody(myResp);   <-----  i changed the response by this method, how can i change the HTTPResponseCode??
oSession.oResponse.headers.HTTPResponseCode = 200;  <-----  this not work

EricLaw

unread,
Sep 12, 2018, 11:51:02 AM9/12/18
to Fiddler
Use e.g.

  oSession.responseCode = 200;

Reply all
Reply to author
Forward
0 new messages