Fiddler AutoResponder should returns a json response with jQuery session id given in request

1.6k Views Asked by At

I need your help, I develop a javascript code that ask a webservice and get back data in JSON format.

When I ask the webservice like this :

https://Server/ServiceEndPoint?id=12345

MyApplication adds some other parameters to my web service URL like this :

https://Server/ServiceEndPoint?id=12345&callback=jQuery18205735686348496944_1459416484049&_=1459416484892

This jQuery session id is generic.

So, I'm using Fiddler to simulate the webservice response. And I need to add this jQuery session Id in the begining of my response like this :

jQuery18205735686348496944_1459416484049({"data":"JSON data"})

Can any one helps me to do this.

Thank you.

4

There are 4 best solutions below

1
NJL On BEST ANSWER

I Used FiddlerScript to do it :

    if (oSession.HostnameIs("ServerName")){

        var body = "";
        var s_qs = (oSession.url + "?").split("?")[1];

        var querystring = HttpUtility.ParseQueryString(s_qs);
        var s_callback = querystring.Get("callback");

        if(oSession.uriContains("EndPointName"))
        {
            body = s_callback + "({'data':'datContent'})";
        }
        oSession.utilSetResponseBody(body);
    }
1
EricLaw On

Fiddler's AutoResponder cannot do this itself (as it cannot modify response bodies based on the request URL). Instead, you'd need to write some simple FiddlerScript to do it. E.g. Rules > Customize Rules > OnBeforeRequest

    if (oSession.uriContains("server/ServiceEndPoint?")) {
        oSession.utilCreateResponseAndBypassServer();
        oSession.oResponse.headers.Add("Content-Type", "application/json");
        oSession.ResponseBody = System.IO.File.ReadAllBytes("C:\\Your\\File.txt");              
        // TODO:Copy the callback value to the front of the response string here.      
    }
0
Raman Zhylich On

Just add this FiddlerScript to OnBeforeResponse and you can continue using AutoResponder. This will fix callback names for you automatically:

    //Autofix all JSONP responses
    if (oSession.uriContains("callback=jQuery")) {

        var s_qs = (oSession.url + "?").split("?")[1];

        var querystring = HttpUtility.ParseQueryString(s_qs);
        var s_callback = querystring.Get("callback");           

        var body = new Regex("jQuery\\d+_\\d+").Replace(oSession.GetResponseBodyAsString(), s_callback);

        oSession.utilSetResponseBody(body);
    }
0
Matt M On

Assuming you're returning static content (file content or whatever), you really want to take the dynamic jquery callback function name and wrap that around whatever you're statically returning. As such, I modified one of the answers above and took care not to affect JSONP responses you aren't targeting.

In Rules > Customize Rules..., do the following:

  1. Add this import at the top of the file if it's not already there: import System.Web;
  2. Find the OnBeforeResponse(oSession) method and add the following:

    //Autofix all JSONP responses
    if (oSession.uriContains("callback=jQuery")) {
        var s_qs = (oSession.url + "?").split("?")[1];
    
        var querystring = HttpUtility.ParseQueryString(s_qs);
        var s_callback = querystring.Get("callback"); 
    
        var oldBody = oSession.GetResponseBodyAsString();
        if (oldBody.StartsWith(s_callback)) return;
    
        var body = String.Concat(s_callback, "(", oSession.GetResponseBodyAsString(), ")");
    
        oSession.utilSetResponseBody(body);
    }
    

This will convert a JSON response of { "test": "testValue" } to jQuery9827345872635_982735982375({ "test": "testValue" }) (the jQuery callback here is random BS I generated for demonstrative purposes).