1

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.
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Yadnesh
  • 21
  • 1
  • 5

1 Answers1

1

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");
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Error invoking bsh method: eval Sourced file: inline evaluation of: ``String Connect = vars.get("92_ConnectionToken").toString(); log.info(Connect); S . . . '' : Typed variable declaration : Method Invocation Connect.replaceAll 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: ``String Connect = vars.get("92_ConnectionToken").toString(); log.info(Connect); S . . . '' : Typed variable declaration : Method Invocation Connect.replaceAll. Still getting above error – Yadnesh May 13 '19 at 06:04
  • @Yadnesh , added to answer `String ConnectToken = Connect.replaceAll("\\+", "%2B");` – Ori Marko May 13 '19 at 06:13
  • 1
    Or, you know, replace() instead of regex-oriented replaceAll() – kumesana May 13 '19 at 06:43
  • replaceAll() is very ill-named. replace() will replace everything too. – kumesana May 13 '19 at 06:45