Referencing user variables not working
Closed this issue · 17 comments
Describe the bug
Referencing a stored user variables in grpc payload is not working
To Reproduce
Steps to reproduce the behavior:
- Make a GRPC request and store a field as variable
kreya.variables.set('example', msg.content.example);
});
- Make another GRPC request, attempting to use stored field in body
{
"example": {{vars.example}}
}
- The request is not valid JSON
Expected behavior
{{vars.example}}
is replaced by content of the stored variable
Environment (if possible, copy the information from the error dialog or the About menu):
- OS: MacOS
- Kreya Version 1.13.1
Additional context
I tried different variable type: object, string... None worked
You need to add double quotes around the variable for the json to be valid.
{
"example": "{{vars.example}}"
}
Also does not work for me, still get JSON error
'F' is invalid after a value. Expected either ',', '}', or ']'. Path: $ | LineNumber: 35 | BytePositionInLine: 44.
That's for example of type string. What if i want to set the whole object?
You can store an object in a user variable, but you must convert it to a valid json format yourself.
Script:
kreya.variables.set("example", { total: 42, average: 23, message: 'hej', title: "test"});
Request json:
{
"example-string": "{{vars.example.message}}",
"example-number": {{vars.example.total}},
"example-object": {
"average": {{vars.example.average}},
"title": "{{vars.example.title}}"
}
}
Note: On the line "example-number", the editor displays an error because it does not know if a number is stored in the variable, but the execution works.
Does this help? Otherwise, you can provide us the stored values in your variable so we can help you further.
If your variable contains a quote "
and you want to apply this in a template like this { "my-message": "{{ vars.my_string }}" }
, this leads to an error since the JSON is no longer valid.
You could work around this by replacing the quote: { "my-message": "{{ vars.my_string }} | string.replace '\"' '\\u0022'" }
Hmm. Yeah I do have quotes in my variable. But it seems to still fail with the work-around. Is there a way to view the rendered json/request after templating so I can debug it?
Also, would it be possible to use the whole object in templating? Given your example
kreya.variables.set("example", { total: 42, average: 23, message: 'hej', title: "test"});
I would want to do something like
{
"example-object": {{vars.example}}
}
as many of my requests contain the whole output of previous requests
Sadly, viewing the rendered request is currently not possible. I added it to our backlog.
For sending the whole object as JSON, maybe something like this could help?
{
"example-object": {{vars.example | object.to_json}}
}
This the object.to_json
function is documented here: https://github.com/scriban/scriban/blob/master/doc/builtins.md#objectto_json
I got Scriban template error
<input>(12,32) : error : The function `object.to_json` was not found
Seems to be in a recent version of Scriban. I guess I'll have to wait for your dependency update.
Also not sure why string.replace
is not working for me, I tried | string.replace '\"' 'gibberish'
and the quote seems to still be there
True, the newer Scriban version might not be yet included in your Kreya version.
Very strange that string.replace
doesn't work... As a further debugging tool, you can try to log the variable content in Scripting with kreya.trace(JSON.stringify(kreya.variables.get('example')))
. This shows up in the Trace tab
Well the variable displays correctly as a string contain a JSON object
"{\"example\": 1}
Now it's working if i directly replace it in script like
kreya.variables.set('example', msg.content.example.replaceAll('\"', '\\u0022'))
But not with the scriban string.replace
thingey.
Ah @CommonGuy , I see. From your example it should have been
{ "my-message": "{{ vars.my_string | string.replace '\"' '\\u0022'}}" }
Just misplaced }}
. Anw it works now, waiting for the Scriban update. ty!
Ah nice that you got it to work!
I'll add "show rendered request" to our roadmap, since that would've helped to debug this
@CommonGuy will there be a version with a newer Scriban version full (probably in beta / alpha?). I'm keen to write some tests using the new collection feature but it's pretty pointless without chaining requests. Many of our requests depend on the whole response of previous requests and it's a bit cumbersome just taking each field out.
A new alpha version was just released, which includes the latest Scriban version. A beta release is expected next week, with the stable release following shortly after
Actually the new object functions are not in the latest release of scriban yet. Back to waiting. Thanks for the effort!
THey just pushed a new tag (5.10.0). Would be really grateful if you up the dep again
@CommonGuy just saw a new alpha but still older Scriban. Sorry for too much tagging but just in case the issue get lost :)
@DominicMCN No problem, we just released a new beta version for you, which includes the latest Scriban tag :)
@CommonGuy holy crap all my end-to-end testing script suddenly works now. Thanks a lot guys! Script is actually really useful now
Glad it works for you!