OnAfterResponse how to determine which API method the response belongs to

Good morning all, I'm struggling a bit with OnAfterResponse.   I know it's global so it will trigger for every API method that is called.    In the screenshot below, you can see that I have two methods for this REST endpoint.   

If I needed to modify the response payload for GetByuuid how would I determine which method is sending the payload to the OnAfterResponse?   I have debugged the OnAfterResponse node but I don't see any method name in the headers or ResponseText, so I'm trying to determine how others normally handle this scenario?


Josh Herron wrote:

Good morning all, I'm struggling a bit with OnAfterResponse.   I know it's global so it will trigger for every API method that is called.    In the screenshot below, you can see that I have two methods for this REST endpoint.   

If I needed to modify the response payload for GetByuuid how would I determine which method is sending the payload to the OnAfterResponse?   I have debugged the OnAfterResponse node but I don't see any method name in the headers or ResponseText, so I'm trying to determine how others normally handle this scenario?


Hi Josh,

I am not aware if there is any way to distinguish the response in OnAfterResponse action based on the method gets called, However, You can try consume only a single method and use OnAfterResponse event to work on that single method's response.


Thanks,

Sachin

Hi Josh,

Just to clearify on Sachin's answer.

You can actually add both the methods separatly instead of adding both the methods under one API node (use consume API method option for second method also instead of using Add Method option from existing API node).

Then you will be able to modify the payload in OnAfterResponse event as both the methods have their separate OnAfterResponse events.

I had thought of consuming the individual methods, and since there are only two it wouldn't be terrible.   However, if the Endpoint had 10 methods that we needed to modify, consuming each of them individually would not be a very elegant solution.   I was hoping there was something that I was just overlooking.

Solution

In that case I think you should use onafterresponseadvance instead of onafterresponse. 

Check below documentation for detailed understanding of this action

https://success.outsystems.com/Documentation/10/Extensibility_and_Integration/REST/Consume_REST_APIs/Advanced_Customizations

https://success.outsystems.com/Documentation/10/Reference/OutSystems_APIs/REST_Extensibility_API_for_.NET

You can use GetActionName method from REST extensibility API explained on above link to know the name of the method in the API which invoked onafterresponseadvance action and update your payload accordingly.

Solution

Hi Josh,

Though it seems you have marked Nikhil's answer as solution, there's an easier way: you can also add an OnBeforeRequest, which very helpfully has, amongst others, the URLPath in its Request Structure. You can then store the Path (and other data if needed) in a Session Variable, and read that Session Variable in the OnAfterResponse, and so determine what Method has been called. 

Kilian Hekhuis wrote:

Hi Josh,

Though it seems you have marked Nikhil's answer as solution, there's an easier way: you can also add an OnBeforeRequest, which very helpfully has, amongst others, the URLPath in its Request Structure. You can then store the Path (and other data if needed) in a Session Variable, and read that Session Variable in the OnAfterResponse, and so determine what Method has been called. 

That's a pretty good solution so long as the web service being called isn't in a service module.   I don't believe you can create session variables in service modules, correct?   


That's true, for Service Modules you cannot use this trick. Most of the time, you could perform some heuristic checks on the Headers or ResponseText though, to see if it contains certain data specific to a certain Method. But if that fails, you need to either seperate the Methods in different REST APIs, as Nikhil has mentioned, or indeed use the OnAfterResponseAdvance.

Hi guys,

I had the same challenge a few months ago. I had several services using session variables but when converting to service modules I couldn’t use the trick anymore.

so I’ve used the OnAfterResponse and consumed one action from the HTTPRequestHandler extension (don’t know now the exact name) to get the request URL. Then I did some parsing and got the rest action that was being executed.


let me know if you need an example.


cheers.

Hi Hélder,

I did some test with that, but those actions return the URL of the Screen (Traditional Web) or Service Action? Can you check which one returns the URL of whatever you are calling?

Kilian Hekhuis wrote:

Hi Hélder,

I did some test with that, but those actions return the URL of the Screen (Traditional Web) or Service Action? Can you check which one returns the URL of whatever you are calling?

Does it make sense to use it for Service Actions?

they are kind of isolated already and you do not have the expanded events (OnBefore, OnAfter..) right?

anyway, my case was on REST APIs.


Hélder,

You misunderstood: we were talking about a Service Action or a Server Action inside a Service Module, calling a REST API. But like I said, in my short test, all GetUrl Actions from the HTTPRequestHandler return the URL of the Screen that calls the Action, not the URL of the REST. I think you are confusing the OnAfterResponse with the OnResponse of an exposed REST API.

Kilian Hekhuis wrote:

Hélder,

You misunderstood: we were talking about a Service Action or a Server Action inside a Service Module, calling a REST API. But like I said, in my short test, all GetUrl Actions from the HTTPRequestHandler return the URL of the Screen that calls the Action, not the URL of the REST. I think you are confusing the OnAfterResponse with the OnResponse of an exposed REST API.

Hi Kilian,


Oh, I see... A service Action, calling a REST API and then get to know which method has been executed from the REST.. Indeed, then the HTTP request will not help, since it holds the original request.


@Josh Herron: Perhaps expand the REST API parameters with a specific one for the method being executed and then OnBeforeRequest get it out of the JSON? (since you want to manipulate it anyway...)

Lots of great ideas being kicked around here.   Thanks all!

I think Kilian's idea about using session variables (if available) is the most straight forward.   However, updating the payload in the OnBeforeRequest would work as well, and then using that in the OnAfterResponse is pretty slick.


That being said, the OnAfterResponseAdvanced does give you access to the GetActionName function which I haven't had a chance to test yet, but appears to solve the problem as well.   So lots of great ways to get at this without having to ingest one method at a time.



If you modify the request in the OnBeforeRequest, there's no way to get that back in the OnAfterResponse, as in the OnBeforeRequest, you are modifying the request (as the name implies), while in the OnAfterResponse you are seeing the response of the remote REST service. There is no way to ask a remote server to copy something from the request and send it back.