英文:
Java object received in AWS API gateway websocket api on connect
问题
我需要将连接 ID 存储在数据库中,因此在对 API 的 $connect
调用中,希望在针对 API 网关 WebSocket 协议中的 Spring Boot Java 控制器中接收包含连接 ID 和消息的负载。
WebSocket URL:wss://xxxx.execute-api.us-west-2.amazonaws.com/test
HttpProxy:https://xxx.xxxxxx.com/create-connection
我决定使用负载作为请求主体对 HttpProxy 进行 POST 调用。
还可以使用 AWS CLI 设置请求参数:
"RequestParameters": {
"integration.request.header.connectionId": "context.connectionId"
}
但仍然无法在参数中获取 connectionId。
英文:
I need to store the connection Id in a database and hence on the $connect
call to the API would like to receive the payload containing the connection id and message in the Spring boot Java Controller in HttpProxy created against $connect
in API gateway WebSocket Protocol.
WebSocket URL: wss://xxxx.execute-api.us-west-2.amazonaws.com/test
HttpProxy: https://xxx.xxxxxx.com/create-connection
<br>
I decided to do a POST call with the payload as the Request body to the HttpProxy.
Also set the Request parameters using the AWS CLI:
"RequestParameters": {
"integration.request.header.connectionId": "context.connectionId"
}
But still not getting the connectionId in the parameters
答案1
得分: 1
一个变通方法是,在这里通过发送连接 ID、请求头来解决我的问题。我们可以将集成请求中的连接 ID 映射到 Http 代理的请求头中。
此外,这个配置目前还不支持从 AWS 控制台进行设置,我们需要使用 aws-cli 进行配置。
参考文档,
aws apigatewayv2 update-integration \
--integration-id abc123 \
--api-id a1b2c3d4 \
--request-parameters 'integration.request.header.connectionId'='context.connectionId'
您还可以转发其他连接细节,详见文档。
您还可以将参数发送到 WebSocket URL,这些参数将原样转发到 Http 代理。
如果您正在使用 wscat
从运行 ZSH 的终端连接到 WebSocket URL,则它无法识别带有参数的 URL。您需要将整个 URL 放在双引号中。
➜ ~ wscat -c "wss://xxxx.execute-api.<region>.amazonaws.com/<stage-name>?param=1¶m2=2"
英文:
A workaround maybe but for sending the connection id, request headers solved the situation for me here. We can map the connection id from the Integration request to the request header of the Http Proxy.
<br>
Also this configuration is not yet supported from the AWS Console rather we need to use the aws-cli to configure this.
<br>
Refering documentation,
aws apigatewayv2 update-integration \
--integration-id abc123 \
--api-id a1b2c3d4 \
--request-parameters 'integration.request.header.connectionId'='context.connectionId'
You can also forward other connection details, refer to the documentation.
<br><br>
You can also send parameters to the websocket URL which will be carry forwarded to the Http Proxy as it is.
<br>
If you are using wscat
to connect to the websocket url from the terminal running ZSH, then it will not recognize the url with parameters. You will need to put the whole url in double quotes.
➜ ~ wscat -c "wss://xxxx.execute-api.<region>.amazonaws.com/<stage-name>?param=1&param2=2"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论