Get a specific value from a response body

Hi guys, i need some help:

How can i get a specific value from a link that i got in a response and set it as a variable? The response is:

{
    "value": "otpauth://totp/BTTExchange:teste123@mailinator.com?algorithm=SHA1&digits=6&issuer=WITExchange&period=30&secret=44RN6GN38TDRXZ5HXDCDA7TTWCB3YWJG"
}

and all i want is to set the value of secret=, being 44RN6GN38TDRXZ5HXDCDA7TTWCB3YWJG as a variable.

Im using:

var body = responseBody;
if(body!=null && body.length>0){
    var array = new RegExp("\"value\":\"(.*?)\"").exec(body);
    if(array!=null && array.length>0 && array[1]!=null){
        pm.globals.set("secret", ""+array[1]);
    }
}

but all i get is the whole β€œvalue”, and not only the secret=.

I know this must be something pretty basic, but im no developer nor do i know how to code.

Thanks in advance!

Hi @shaga85

Just change your regex a little bit;

var body = responseBody;
if(body!=null && body.length>0){
    var array = new RegExp("secret\=(.*?)\"").exec(body);
    if(array!=null && array.length>0 && array[1]!=null){
        //pm.globals.set("secret", ""+array[1]);
        console.log(array[1]);
    }
}
1 Like

Nice, thanks a bunch! It worked :slight_smile:

1 Like