Forum Discussion

amirse's avatar
3 years ago

Replace one part in a request with the value from an variable

Hi,   Is there a way to replace the "XXXX" in the method below with the value of the variable replaceToken?   def replaceToken = "username" parsedJson.secrets.XXXX.toString()   I.e., I would l...
  • ChrisAdams's avatar
    3 years ago



    Have a look at


    You can call methods using variables and array elements.


    I've created a small Groovy script as a demo.  See below.


    def myObj = new MyClass();
    // Call in the usual way."Call in the usual way.");;;;"-------------------------------------------");
    // Call using a variable."Call using a variable");
    def methodToCall = "methodOne";"$methodToCall"());
    methodToCall = "methodTwo";"$methodToCall"());
    methodToCall = "methodThree";"$methodToCall"());"-------------------------------------------");"Call with method name in an array");
    def myArray = ["methodOne", "methodTwo", "methodThree"];[0])());[1])());[2])());"-------------------------------------------");
    class MyClass {
    		// empty constructor
    	def methodOne(){
    		return "I am method one";
    	def methodTwo(){
    		return "I am method two";
    	def methodThree(){
    		return "I am method three";