I'm extracting some value using regular expression say "Ehj113GPTAw9RMbTgxce2jqh+2rn7tLWFz5JHyhxa+K68ksbE3O5MPvvLU4ihAeaE08DlO7X04MpSVRP7jZIHGv0qPzdmJgwXZUOmVwFWNTQZbpH"

I want to replace the '+' with '%2B' and send it to the second request as "Ehj113GPTAw9RMbTgxce2jqh%2B2rn7tLWFz5JHyhxa%2BK68ksbE3O5MPvvLU4ihAeaE08DlO7X04MpSVRP7jZIHGv0qPzdmJgwXZUOmVwFWNTQZbpH"

How can I achieve this?

I have already tried replaceAll and replace but I'm going wrong somewhere.

var Connect = vars.get("92_ConnectionToken").toString();
log.info(Connect);
String ConnectToken = Connect.replaceAll("+", "%2B");
String ConnectFinal = ConnectToken.replaceAll("/", "%2F");
vars.put("92_ConnectionToken", ConnectFinal);
log.info(ConnectFinal);

Expected result:

Ehj113GPTAw9RMbTgxce2jqh%2B2rn7tLWFz5JHyhxa%2BK68ksbE3O5MPvvLU4ihAeaE08DlO7X04MpSVRP7jZIHGv0qPzdmJgwXZUOmVwFWNTQZbpH.

This string should be sent as the input to the second request.

Actual Result:

2019-05-13 10:37:22,343 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: `` var Connect = vars.get("92_ConnectionToken").toString(); log.info(Connect); Str . . . '' : Typed variable declaration : Method Invocation Connect.replaceAll
2019-05-13 10:37:22,343 WARN o.a.j.m.BeanShellPreProcessor: Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   Sourced file: inline evaluation of: `` var Connect = vars.get("92_ConnectionToken").toString(); log.info(Connect); Str . . . '' : Typed variable declaration : Method Invocation Connect.replaceAll.

1 Answers

1
user7294900 On

If you want to use String specific methods as replaceAll, define variable as String:

String Connect = vars.get("92_ConnectionToken");

You need to escape regex special characters as +

String ConnectToken = Connect.replaceAll("\\+", "%2B");

As @kumesana commented, you better use replace method:

String ConnectToken2 = Connect.replace("+", "%2B");