http:RequestContext VS http:Request

29 Views Asked by At

I am using a request interceptor to perform JWT validation and decoding. I need to pass the decoded custom JWT information from the Interceptor to the resource endpoint (service.bal). I have identified that there are 2 possible ways to do this. Passing through a http:RequestContext Passing through a http:Request I just wanted to clarify which way is the ideal way for the above requirement and the reason for it.

public type JwtRecord record {|
    string email;
    string[] roles;
|}
1

There are 1 best solutions below

0
On

The request context serves the purpose of sharing contextual information throughout the request-response path. Therefore, using a request context is ideal here, rather than unnecessarily creating a request object. You can set the value in the context like this:

ctx.set("JWT_INFO", jwtInfo);

And retrieve the info from the context like this:

JwtRecord jwtInfo = check ctx.getWithType("JWT_INFO");