Scala - how to use variables in a multi-line string literal
Asked Answered
R

2

6

I want to call value of 'myActionID' variable. How do I do that? If i pass static value like "actionId":1368201 to myActionID then it works, but If I use "actionId" : ${actionIdd} it gives error.

Here's the relevant code:

class LaunchWorkflow_Act extends Simulation {

    val scenarioRepeatCount = 1
    val userCount = 1
    val myActionID = "13682002351"
    
    val scn = scenario("LaunchMyFile")
        .repeat (scenarioRepeatCount) {
            exec(session => session.set("counter", (globalVar.getAndIncrement+" "+timeStamp.toString())))
            .exec(http("LaunchRequest")
            .post("""/api/test""")
            .headers(headers_0)
            .body(StringBody(
                """{    "actionId": ${myActionID} ,
                "jConfig": "{\"wflow\":[{\"Wflow\":{\"id\": \"13500145349\"},\"inherit-variables\": true,\"workflow-context-variable\": [{\"variable-name\": \"externalFilePath\",\"variable-value\": \"/var/nem/nem/media/mount/assets/Test.mp4\"},{\"variable-name\": \"Name\",\"variable-value\": \"${counter}\"}]}]}"
                }""")))

            .pause(pause) 

        }
    }

setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)

Everything works fine If I put value 13682002351 instead of myActionID. While executing this script in Gatling I am Getting this error

ERROR i.g.http.action.HttpRequestAction - 'httpRequest-3' failed to execute: No attribute named 'myActionID' is defined

Raama answered 16/5, 2016 at 12:10 Comment(2)
Try: "actionId": ${myActionID}Eberta
@antikantian: Tried with your comment but error shows " No attribute named 'myActionID' is defined"Raama
F
16

Scala has various mechanisms for String Interpolation (see docs), which can be used to embed variables in strings. All of them can be used in conjunction with the triple quotes """ used to create multi-line strings.

In this case, you can use:

val counter = 12
val myActionID = "13682002351"
val str = s"""{    
                "actionId": $myActionID ,
                "jConfig": "{\"wflow\":[{\"Wflow\":{\"id\": \"13500145349\"},\"inherit-variables\": true,\"workflow-context-variable\": [{\"variable-name\": \"externalFilePath\",\"variable-value\": \"/var/nem/nem/media/mount/assets/Test.mp4\"},{\"variable-name\": \"Name\",\"variable-value\": \"${counter}\"}]}]}"
              }"""

Notice the s prepended to the string literal, and the dollar sign prepended to the variable names.

Finicking answered 16/5, 2016 at 15:14 Comment(7)
I am not able to compile the code after adding the line you mentioned val str = s"""...."""Raama
Doesn't work, it gives error status.find.in(200,304,201,202,203,204,205,206,207,208,209), b 1 (100.0%) ut actually found 400. Any other way?? have tried with almost all the combinations, "actionId": $actionIdD, "actionId": ${actionIdD}, "actionId: $actionIdD", "actionId": + actionIdD, s"actionId: $actionIdD", s"actionId": $actionIdD but gives the same errorRaama
Try testing the string creation separately from the Gatling request - you might be looking at the wrong place. The code I pasted executes successfully in Scala REPL and inserts the value of myActionID - I can't vouch for anything else (correctness of json, any other code) as there's not enough context, and your error might be coming from there...Finicking
I think this is not server side or Json related issue, as It works when I pass 13682002351 as actionID in .body, while gives error when use variable name.Raama
did you add the s before the triple-quote sign (not before the single-quote encapsulating actionId)?? I can't see how this can fail, the code pasted above works in REPL and produces a string starting with { "actionId": 13682002351 , "jConfig":...Finicking
I have posted code in answer with list of errors in "I did this, val actionId = "1368225424501" with errors. Can you please check. Thanks :-)Raama
if I use your code val str = s"""{ "actionId": $myActionID ,...} then it shows ${counter} error as it is declared in exec(session => session.set("counter",...) and we are calling it before declaration, so I am not able to call that 'str' in '.body'. any other solution for this?Raama
L
7

Using S interpolated String we can do this easily:

 s"""Hello Word , Welcome Back!
      How are you doing ${userName}"""
Larissa answered 28/5, 2020 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.