Success Template - URL Encoding

Hi - I have a data action that is invoked from a script and in the data action response is a URL that includes parameters. I then want to use that response url again in the script and need the response url properly encoded.

I have tried to encode the url via the data action success template but without success - is this possible or do the velocity macros only work on the request side? The data action request itself to the endpoint is successful and I can see the elements I need all populated, and everything is ok without my attempts to encode the response urls.

Thanks in advance

If you are asking if you can use a data action input in the final response, the answer is currently no. There is an idea for adding that here:

https://genesyscloud.ideas.aha.io/ideas/OTB-I-405

Thanks Jason, not looking to use an input in the output, and looking to use an encode function in the output which doesn't seem to be supported?

All of the encoding functions are available in the response template. What exactly is going wrong?

Here is the response template I am using:

{
  "translationMap": {
    "sessionId": "$.sessionId",
    "customerId": "$.customerId",
    "customerURL": "$.customerURL",
    "agentURL": "$.agentURL"
  },
  "translationMapDefaults": {
    "sessionId": "\"\"",
    "customerId": "\"\"",
    "customerURL": "\"\"",
    "agentURL": "\"\""
  },
  "successTemplate": "{\n\"sessionId\": ${sessionId}, \"customerId\": ${customerId}, \"customerURL\": ${customerURL}, \"agentURL\": ${agentURL}}"
}

This works.
The two output urls have url parameters which I would want to escape before passing around but I have struggled to use a macro without generating an error in step 10 of the test:

10. Apply output transformation: Transform failed to process result using 'successTemplate' template due to error:'Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: REDACTED ...

Can you suggest how I escape the urls - I may have the wrong macro but here is what I am trying:

"successTemplate": "{\n\"sessionId\": ${sessionId}, \"customerId\": ${customerId}, \"customerURL\": $esc.urlFormEncode(${customerURL}), \"agentURL\": $esc.urlFormEncode(${agentURL})}"

I am playing with this now, but if you have a chance can you provide an example of the input that is failing. Feel free to change anything sensitive, but please make sure to keep anything like symbols or spaces.

The issue might be that you are trying to include the quotes in the customerURL and agentURL variables, instead of putting the quotes in the successTemplate like this:

"successTemplate": "{\n\"sessionId\": ${sessionId}, \"customerId\": ${customerId}, \"customerURL\": \"$esc.urlFormEncode(${customerURL})\", \"agentURL\": \"$esc.urlFormEncode(${agentURL})\"}"

--Jason

Thanks @Jason_Mathison, that has certainly moved me forward and I can now get the response. A little bit of trial and error and possibly one macro more than I need, I am here:

"successTemplate": "{\n\"sessionId\": ${sessionId}, \"customerId\": ${customerId}, \"customerURL\": \"$!esc.jsonString($esc.html(${customerURL}))\", \"agentURL\": \"$!esc.jsonString($esc.html(${agentURL}))\"}"

My output in this case is (for customerURL) "https://my.host.com/Default.aspx?mt=self&sid=1234567890"

... so I just need to work out how to remove the " on each end, I am presuming this is carried in from the translationMap and with your suggestion I expect I now have two double quotes at the start and the end. The key part is the encoding of the & in the middle as this was where the response was truncated when passed into the agent script - this data action is being called from there as well as from Architect.

Any thoughts?

Here is a sample response from the service I am calling:

{
  "agentURL": "https://my.host.com/AgentDesktop.aspx?sid=1234567890",
  "customerId": "a66839d2-c37a-4973-9dc8-2d2d9d01d34c",
  "sessionId": "1234567890",
  "customerURL": "https://my.host.com/Default.aspx?mt=self&sid=1234567890"
}

Many thanks,

hey sorry for asking but really you got you solution because I think encoding the response URL directly within the data action success template is currently not supported. If I'm wrong, then please let me know too.