130
Views
4
Comments
How to get a unique session id from consuming REST API

From the above image, I want to get some Session Id when I perform REST on OnBeforeRequest and OnAfterResponse events. It's used for storing as an Id which uses for logging my external consume the integration.

I try to use PlatformRuntime_API dependency and it doesn't work as I expect

For the designed flow, I add logger action into both OnBeforeRequest and OnAfterResponse events which need to use some Id to define that response of consuming is the same request because OnBeforeRequest and OnAfterResponse events are global events which I can't track specific requests.

Does anyone have a solution to this problem or any I dea to log requests and responses with all parameters like this?

2021-03-18 21-03-15
Benjith Sam
 
MVP

Hi,

Is this what you are looking for?

HTTPRequestHandler > GetSessionId


Kind regards,

Benjith Sam

2022-08-03 11-50-19
Pongsakorn.P

I already try this action but it always returns an empty session id output

2021-05-18 02-27-17
Manish Gupta
Champion

Hello Pong

There is a general method to obtain a unique session ID when consuming a REST API in OutSystems and use it for logging purposes, you can follow these steps:

Step 1: Add the "PlatformRuntime_API" dependency to your module. To do this, open your module, go to the "Manage Dependencies" screen, search for "PlatformRuntime_API," and add it to your module.

Step 2: In the "OnBeforeRequest" event of your REST API integration, use the "PlatformRuntimeAPI.GenerateGUID" action to generate a unique session ID. This action generates a globally unique identifier (GUID) that you can use as your session ID.

Example code for the "OnBeforeRequest" event: Session.Id = PlatformRuntimeAPI.GenerateGUID()

Here, `Session.Id` is an attribute or variable where you can store the generated session ID.

Step 3: In the "OnAfterResponse" event, you can use the generated session ID as needed for logging or any other purpose.

Example code for the "OnAfterResponse" event: LogAPIResponse(Session.Id, Response.Content)

In this example, `LogAPIResponse` is a custom function or action that you define to log the API response. You pass the session ID (`Session.Id`) and the API response content (`Response.Content`) as parameters to the logging function.

By generating a unique session ID in the "OnBeforeRequest" event and storing it in a variable or attribute, you can access and use it throughout the integration, including the "OnAfterResponse" event, for your logging purposes.


I hope the solution will help you to workaround the challenge you are facing. 

Thanks

2022-08-03 11-50-19
Pongsakorn.P

Hello Manish,

Thank you for the idea. I got some solutions with the GetRequestContent action of the HTTPRequestHandler dependencies by collecting the RawContent output parameter from that action which contain structure as

{"requestKey":"32db6bb6-6630-443d-afe3-128b3afaa94f","inputParameters":{"Service":"Localize","PackageCode":"TST230418573Test03","PackageName":""}}

By this, I will receive the unique requestKey parameter per server request and use it to log the request and response.

From your idea, the site property which contains the session id might not work properly. Because it acts like a shared variable and if it has heavy incoming requests, it might collect the wrong id.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.