Using response from one REST resource in another


(Kieran) #1

Hello again,

I have 2 REST calls that need to be made.
Call 1 uses a query in the URL to replace part of its request/URL.
This works fine and I get the intended response and manipulate it to give me a string.

This string from REST 1 now needs to form part of the REST 2 call, to a slightly different request/URL.
I see @Bart has previously spoken about using REST Triggers to store session variables, but I do not know if this would work in my case or what action to call the trigger on.

Example: User visits my-site.com/rest-call?param=abc .
This asset runs: my-api.com/query-param=%globals_get_param%
Returns “12345”
That “12345” needs to form part of second, different REST call: my-api.com/query-2-param={....} <== I need to insert the output from call 1 into this. The “12345” in this example.

I tried using the SSJS of the first call, to run the second call, but I cant work out how to pass the variable, as I can only think of using ‘%globals_asset_contents_raw:my-second-query%?query-2-param=’+firstResult and this runs the second call and returns it before appending the ‘?query-2-param…’ to the end.


(Kieran) #2

@JohnGill tagging you as you have experience with REST assets.
Please excuse the ask for help, but I have a tight deadline for this one and I’m stuck.


(Bart Banda) #3

Hey Kieran,
I’m pretty sure you should be able to do this via a single Trigger.
So set up a trigger that fires on “Asset Accessed” event with a condition of that particular asset being hit, and probably include a query parameter condition to, as you want to limit the risk of the trigger firing unecessarily as much as possible.
Then in your actions, add 2 separate Call REST Resource actions, the first one that puts the result of the the manipulated response into a session var, that you should then be able to access with %globals_session_yourcustomvar% in the second REST action.

Let me know how you go.


(Kieran) #4

Thanks @Bart,

  1. I’ve done the following, but the server doesnt respond, event after 30seconds.
  2. The error log also shown below.
  3. How do I output this? Where would a print command output?

(/core/include/mysource.inc:6849) - session_write_close(): Failed to write session data (redis). Please verify that the current setting of session.save_path is correct (tcp://maeqcet37wvokfr.xoaugg.ng.0001.apse2.cache.amazonaws.com:6379?weight=1&amp;amp;timeout=2000&amp;amp;database=1)

Worth noting, the first call works fine when using a REST asset and the second works fine if I input the variable content manually into the call.

Thanks in advance.


(Kieran) #5

Ok, I noticed I had two things ticked by accident, which was probably looping.
It now seems to work ok, but how do I access the output?
By making a session variable from that and then calling it in SSJS in the page I am accessing?
I will now try.


(Bart Banda) #6

Yeah so in your second REST call you can also add a custom session var to store whatever desired output of the HTTP response you want there (you can use the JAVASCRIPT CODE section to manipulate the response before you store it as a session var). So lets say you name that your second session var “mycustomvar2”, you can then print that on your standard page (or via paint layout depending on your implementation) as

%globals_session_mycustomvar2%

Either to print directly to the frontend or to inject into some extra SSJS you might want to run on the rendering layout (Standard page or PL). Hope that makes sense?


(Kieran) #7

@Bart; a gentleman and a scholar.
I owe you a beer.