Forum Discussion

TerryHe's avatar
TerryHe
New Contributor
9 years ago

How to get value from a Map returned by Groovy Script in Request

In the groovy script step, i returned a Map type value as:

===========================

def serverSessionIdVal = "${java.util.UUID.randomUUID()}";
def clientSessionIdVal = "${java.util.UUID.randomUUID()}";

vars = [:]
vars["sid"] = serverSessionIdVal
vars["cid"] = clientSessionIdVal

return vars

===========================

 

In the REST Request body, we can use place holder "${DefineVars#result}" to access the whole map, while how can i access one of the value like vars["cid"]

  • Hi,

     

    With the specific approach you have taken I am uncertain what syntax to use at this point. However, I would normally do what you hopefully want a slightly different way using the context map to hold your custom map:

    def serverSessionIdVal = "${java.util.UUID.randomUUID()}";
    def clientSessionIdVal = "${java.util.UUID.randomUUID()}";
    vars = [:]
    vars["sid"] = serverSessionIdVal
    vars["cid"] = clientSessionIdVal
    context.vars=vars
    return vars

    Then you can access the map values in a request with the following syntax:

    ${=vars.cid}

    and

    ${=vars.sid}

    Regards,
    Rupert

     

     

  • rupert_anderson's avatar
    rupert_anderson
    Valued Contributor

    Hi,

     

    With the specific approach you have taken I am uncertain what syntax to use at this point. However, I would normally do what you hopefully want a slightly different way using the context map to hold your custom map:

    def serverSessionIdVal = "${java.util.UUID.randomUUID()}";
    def clientSessionIdVal = "${java.util.UUID.randomUUID()}";
    vars = [:]
    vars["sid"] = serverSessionIdVal
    vars["cid"] = clientSessionIdVal
    context.vars=vars
    return vars

    Then you can access the map values in a request with the following syntax:

    ${=vars.cid}

    and

    ${=vars.sid}

    Regards,
    Rupert

     

     

    • nmrao's avatar
      nmrao
      Champion Level 3
      TerryHe, note that you need to execute the test case in order to make this work. If you run groovy script, and rest request separately, then it won't work.
    • TerryHe's avatar
      TerryHe
      New Contributor

      Thanks Rupert, that works! :robothappy: