我一直在努力通过aws提供的回调url(https://******.execute-api.us-east-1.amazonaws.com/test/@connections)向连接的客户端发送响应。
在我的后端中有以下代码来发送响应,这是一种单向通信。
AwsClientBuilder.EndpointConfiguration config =
new AwsClientBuilder.EndpointConfiguration("https://*********.execute-api.us-east-1.amazonaws.com/test", "us-east-1");
AmazonApiGatewayManagementApi client = AmazonApiGatewayManagementApiClientBuilder.standard()
.withEndpointConfiguration(config).build();
PostToConnectionRequest request = new PostToConnectionRequest();
request.withConnectionId("bGYcWek5oAMCKwA=");
Charset charset = Charset.forName("UTF-8");
CharsetEncoder encoder = charset.newEncoder();
ByteBuffer buff = null;
try {
buff = encoder.encode(CharBuffer.wrap("Hello from lambda"));
} catch (CharacterCodingException e) {
log.error("Error while encoding", e);
}
request.withData(buff);
PostToConnectionResult result = client.postToConnection(request);
log.info("Result of websocket send:\n" + result);
Run Code Online (Sandbox Code Playgroud)
获取服务:AmazonApiGatewayManagementApi;状态代码:403;错误代码:InvalidSignatureException异常。
注意:我已经设置了所有内容,例如证书和 aws 访问密钥。
正如 aws 文档中提到的,我尝试过使用awscurl,它有效。我可以在 websocket 连接的客户端上看到响应。 https://docs.amazonaws.cn/en_us/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-connections.html
我有一个带有LAMBDA_PROXY集成请求类型的API网关.在Lambda中调用context.succeed时,响应头部按预期发送回代码302(如下所示).但是,我想处理500和404错误,到目前为止我唯一确定的是,我正在错误地返回错误,因为我收到了502 Bad Gateway.我的context.fail出了什么问题?
这是我的handler.js
const handler = (event, context) => {
//event consists of hard coded values right now
getUrl(event.queryStringParameters)
.then((result) => {
const parsed = JSON.parse(result);
let url;
//handle error message returned in response
if (parsed.error) {
let error = {
statusCode: 404,
body: new Error(parsed.error)
}
return context.fail(error);
} else {
url = parsed.source || parsed.picture;
return context.succeed({
statusCode: 302,
headers: {
Location : url
}
});
}
});
};
Run Code Online (Sandbox Code Playgroud)