0

How to edit body before send request with Fiddler(script)

in my case path /login have body username: xxx pass: xxxx

how to edit user pass before send send request

Teerapong
  • 1
  • 1
  • Hi! Welcome to Stackoverflow! Please show what you have tried so we can help you! – Hauns TM Jan 12 '21 at 21:35
  • i don't know command to set req boby sir – Teerapong Jan 13 '21 at 07:26
  • i can set header by oSession.oRequest["NewHeaderName"] = "New header value"; but cannot set body request – Teerapong Jan 13 '21 at 07:36
  • Well, since I do not know exactly what you are looking for, this might not be spot on for you. Anyway, a very quick lookup with Google gave me this: http://jonathanblog2000.blogspot.com/2013/09/set-username-and-password-for-basic.html – Hauns TM Jan 14 '21 at 06:53

2 Answers2

1
static function OnBeforeRequest(oSession: Session) {
    var loginDomain = 'www.testlogin.org';  
    var loginPath = '/login';
    var username;
    var password;
    var strBody
    
    if (username == null && oSession.uriContains(loginDomain) && 
        oSession.uriContains(loginPath))
    {

        username = FiddlerObject.prompt("Enter user name: ");
        password = FiddlerObject.prompt("Enter password: ");
        strBody='username: ' + username + ' pass: ' + password;
        //encode the body to handle special characters in the password
        //password "P&ssword=secure"    will be    "P%26ssword%3Dsecure"                                            
        strBody=Utilities.UrlEncode(strBody);
        oSession.utilSetRequestBody(strBody);
    }

//... the rest of the OnBeforeRequest function
}

This will open 2 prompt windows to enter the username and then password after entering the login URL in a browser and executing a request. The prompts may not popup in front of the browser, you may need to switch focus to fiddler to use the prompt windows

jrainey
  • 19
  • 5
0

For modifying requests in Fiddler classic use the OnBeforeResponse function. To replace username and password in body of the HTTP request (not in header as used e.g. by BASIC auth) you can use utilReplaceInRequest method which performs search and replace on text level:

static function OnBeforeResponse(oSession: Session) {
    // check if the requests is to the correct hosts and path
    if (oSession.HostnameIs("www.example.org") && oSession.PathAndQuery.Contains("/login")) {
        oSession.utilDecodeResponse();
        oSession.utilReplaceInRequest("username: xxx", "username: newusername");
        oSession.utilReplaceInRequest("pass: xxxx", "pass: newpassword");
    }
}

Alternatively you can get the body as text and manipulate it as you want using standard .Net String methods:

    if (oSession.HostnameIs("www.example.org") && oSession.PathAndQuery.Contains("/login")) {
        oSession.utilDecodeResponse();
        var body = oSession.GetRequestBodyAsString();
        // use .Net String manipulation methods to find and replace text in the body
        oSession.utilSetRequestBody(body);
    }
Robert
  • 39,162
  • 17
  • 99
  • 152
  • how can i set when i don't know value of object i want to set like this obj["username"] = "teeraphong" not use replace method sir because i don't know value to replace – Teerapong Jan 18 '21 at 04:50