Reputation: 1747
I am using a Websocket api in WSO2. I need to pass custom header to my backend Websocket service. I have found a documentation here https://docs.wso2.com/display/AM210/Adding+Mediation+Extensions
but it's for rest api call not for Websocket. So how can i send custom header to my backend Websocket service through WSO2.
Upvotes: 1
Views: 651
Reputation: 1100
You can send a custom header to the Web Socket backend in the initial WebSocket handshake. you can set it in the following format to the client handshake request.
websocket.custom.header.<required-header-name>
Ex: If the expected header is X-JWT-Assertion, the header that should be sent is
websocket.custom.header.X-JWT-Assertion
This feature support is added from API Manager v2.6.0
You could not use mediation sequences here since the rest of the communication is done with ws frames.
Adding more information.
Here is an example of a netty based web socket client which can be used to communicate with the WS API deployed in the API Manager. [1]
The authorization header is set in the Handshake as follows.
final WebSocketClientHandler handler = new WebSocketClientHandler(
WebSocketClientHandshakerFactory.newHandshaker(
uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders()
.add("Authorization", "Bearer e2238f3a-e43c-3f54-a05a-dd2e4bd4631f")));
This Authorization header is to authenticate with API Manager. If you need to send a custom header, you can add another header by modifying the above example as follows.
DefaultHttpHeaders headers = new DefaultHttpHeaders();
headers.add("Authorization", "Bearer e2238f3a-e43c-3f54-a05a-dd2e4bd4631f");
headers.add("websocket.custom.header.X-WS-UserName", "bob");
final WebSocketClientHandler handler = new WebSocketClientHandler(
WebSocketClientHandshakerFactory.newHandshaker(
uri, WebSocketVersion.V13, null, false, headers));
This header will be sent to the backend as,
X-WS-UserName : bob
Upvotes: 2