Changing request body with fiddlerScript

5,641 views
Skip to first unread message

Avner Aviram

unread,
Mar 24, 2014, 7:50:54 AM3/24/14
to httpf...@googlegroups.com
Hi
i'm trying to change a request body, before it is being sent to the server
i'v tried:

static function OnBeforeRequest(oSession: Session) {
 if (oSession.uriContains("Part of my URL")){
          oSession.utilReplaceInRequest("X", "Y");
      }
}
(in this example, trying to replace X with Y in the request body)

it doesn't work. the original request is being sent (with X, not Y).

when i do the same in the OnBeforeResponse method (when trying to change a response bodey),it works fine, the response body is being changed.

any suggestions?

EricLaw

unread,
Mar 24, 2014, 1:17:01 PM3/24/14
to
The code you've provided is generally correct, assuming that the request body in question truly contains "X". Can you share a SAZ file showing this traffic, or copy/paste it here?

(In particular, be sure that the "X" you're looking for is in the request's POST/PUT/whatever body and not as a component of the url's querystring. )


Avner Aviram

unread,
Mar 25, 2014, 11:57:26 AM3/25/14
to httpf...@googlegroups.com
Ok, sorry for the confusion.. but what i'm actually trying to do is to change the request's query string, not the request body (as i wrote above). the request is a GET request, not POST, and it has no body. although the topic of this post is now somehow irrelevant, ill present my current problem. 
i'v encountered this method;
if (oSession.PathAndQuery=="/version1.css") {
      oSession.PathAndQuery="/version2.css";

the thing is, that i want to change a part of the query string , and i don't see a way to do it with this method. moreover, the query string is dynamic, meaning, parts of it changes and i can't predict their values. i only want to modify a certain part of the string which is constant. 

any ideas?

EricLaw

unread,
Mar 25, 2014, 12:03:34 PM3/25/14
to httpf...@googlegroups.com
Hi, Avner--

I think you can boil your question down to "How do I change part of a string in JScript.NET?"

There are two ways:

1> Via the String.Replace method
2> Via Regular Expressions.

Using the Replace method is simpler for most scenarios.

if (oSession.HostnameIs("siteICareAbout.com") && oSession.uriContains("searchstring"))
{
   var sPQ = oSession.PathAndQuery;
   sPQ = sPQ.Replace("searchstring", "replacestring");
   oSession.PathAndQuery = sPQ;
}

Avner Aviram

unread,
Mar 26, 2014, 4:35:02 AM3/26/14
to httpf...@googlegroups.com
Hi Eric
Your example works, i used the replace method, and got exactly what i wanted to achieve.
Thanks!


Слава Фартовий

unread,
Jul 15, 2017, 1:34:13 PM7/15/17
to Fiddler

Hello .. you can help me with the script. The thing is that I nada change the values in Response .. I found a script similar on the site but it did not give me any efect.


if (oSession.HostnameIs("www.bayden.com") && oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){ oSession.utilDecodeResponse(); oSession.utilReplaceInResponse('<b>','<u>'); } How can you rewrite it so that it changes values?

Eric Lawrence

unread,
Jul 15, 2017, 3:22:16 PM7/15/17
to Fiddler
There are two common causes for a response modification appearing not to work:

1. Your filter criteria aren't hit.
2. Your response was already streamed.

For #1, change the script to 


if (oSession.HostnameIs("www.bayden.com") &&
oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){
oSession["ui-backcolor"] = "yellow"; oSession.utilDecodeResponse(); oSession.utilReplaceInResponse('<b>','<u>');
}

That way, you can confirm the script is even running on the Session in question (as its background will change to yellow).

For #2, there are various approaches, but the simplest thing to start with -- Is the Stream button depressed on Fiddler's toolbar? If so, unclick it.

Слава Фартовий

unread,
Jul 16, 2017, 8:47:21 AM7/16/17
to Fiddler

Does not want something to change it ... maybe some other script is needed .. I'm just nada that he changed the meaning. As shown on the screen



   //FiddlerApplication.Log.LogFormat("Session {0}: Response header peek shows status is {1}", oSession.id, oSession.responseCode);
        if (m_DisableCaching) {
            oSession.oResponse.headers.Remove("Expires");
            oSession.oResponse["Cache-Control"] = "no-cache";
        }

        if ((bpStatus>0) && (oSession.responseCode == bpStatus)) {
            oSession["x-breakresponse"]="status";
            oSession.bBufferResponse = true;
        }
        
        if ((null!=bpResponseURI) && oSession.uriContains(bpResponseURI)) {
            oSession["x-breakresponse"]="uri";
            oSession.bBufferResponse = true;
        } 
if (oSession.HostnameIs("https://e-fb2.islandsville.com") && 
oSession.oResponse.headers.ExistsAndContains("Content-Type","text/html")){
oSession["ui-backcolor"] = "yellow";
oSession.utilDecodeResponse();
oSession.utilReplaceInResponse('"1925":0,','"1925":1100,');
}
    }    

    static function OnBeforeResponse(oSession: Session) {
        if (m_ShowTimestamp){
            oSession["ui-customcolumn"] = DateTime.Now.ToString("H:mm:ss.ffff") + " " + oSession["ui-customcolumn"]; 
        }

        if (m_ShowTTLB){
            oSession["ui-customcolumn"] = oSession.oResponse.iTTLB + "ms " + oSession["ui-customcolumn"]; 
        }

        if (m_Hide304s && oSession.responseCode == 304){
            oSession["ui-hide"] = "true";
        }

    }

Eric Lawrence

unread,
Jul 16, 2017, 8:59:32 AM7/16/17
to Fiddler
A hostname does not contain a protocol.

Instead of

   oSession.HostnameIs("https://e-fb2.islandsville.com") 

use

oSession.HostnameIs("e-fb2.islandsville.com") 

Слава Фартовий

unread,
Jul 17, 2017, 3:34:35 AM7/17/17
to Fiddler

Hi .. creak started to work. But he does not change the meaning of what I need. The photo shows that he puts ... but the fact that I need to change does not want him.
I am writing to you from google translator)
 } 
if (oSession.HostnameIs("e-fb3.islandsville.com") && 
oSession.oResponse.headers.ExistsAndContains("Content-Type","json")){
oSession.utilDecodeResponse();
oSession.utilReplaceInResponse('<1925=0>','<1925=1000>');
}
if (oSession.HostnameIs("e-fb1.islandsville.com") && 
oSession.oResponse.headers.ExistsAndContains("Content-Type","json")){
oSession.utilDecodeResponse();
oSession.utilReplaceInResponse('<1925=0>','<1925=1000>');
}
if (oSession.HostnameIs("e-fb2.islandsville.com") && 
oSession.oResponse.headers.ExistsAndContains("Content-Type","json")){
oSession.utilDecodeResponse();
oSession.utilReplaceInResponse('<1925=0>','<1925=1000>');

Eric Lawrence

unread,
Jul 17, 2017, 1:49:20 PM7/17/17
to Fiddler
Do this:

if (oSession.hostname.StartsWith("e-fb") && oSession.hostname.EndsWith(".islandsville.com") && 
   oSession.oResponse.headers.ExistsAndContains("Content-Type","json")) {

    oSession["ui-bold"] = "true";
    oSession["ui-backcolor"] = "yellow";
    oSession.utilDecodeResponse();
    oSession.utilReplaceInResponse('<1925=0>','<1925=1000>');
}

See if the expected lines are bold. If not, then your criteria are incorrect.

Слава Фартовий

unread,
Jul 17, 2017, 5:03:02 PM7/17/17
to Fiddler

I took a video especially for you .. that you understood what the problem is .. Here is the silk on youtube)

https://youtu.be/ZUE79DYG5qQ

Eric Lawrence

unread,
Jul 17, 2017, 5:10:01 PM7/17/17
to Fiddler
As shown at timestamp 6:11 in your video, the text string in the response isn't <1925=0>, it's "1925":0. So your code should look something like:

if (oSession.hostname.StartsWith("e-fb") && oSession.hostname.EndsWith(".islandsville.com") && 
   oSession.oResponse.headers.ExistsAndContains("Content-Type","json")) {

    oSession["ui-bold"] = "true";
    oSession["ui-backcolor"] = "yellow";
    oSession.utilDecodeResponse();
    oSession.utilReplaceInResponse('"1925":0','"1925":1000');
}

Слава Фартовий

unread,
Jul 18, 2017, 9:55:27 AM7/18/17
to Fiddler

Here so it turns out.

The game does not work (


Maybe some other way to change the code?

Eric Lawrence

unread,
Jul 18, 2017, 11:08:52 AM7/18/17
to Fiddler
"does not work" unfortunately doesn't tell me anything useful.

The text in your screenshot has not been run through Fiddler's utilDecodeResponse() method (I can tell because the HTTP Chunked Encoding length values are still at the top and bottom of the content).

Слава Фартовий

unread,
Jul 18, 2017, 3:56:04 PM7/18/17
to Fiddler
Is there another way to change the HTML code?
Reply all
Reply to author
Forward
0 new messages