Search code examples
spring-cloud-function

Getting Request headers with FunctionInvoker in AWS Adapter


The headers are available in the Http Request when it's sent from the AWS API gateway and received by the FunctionInvoker. It looks like the code specifically ignores them if it matches:

Message requestMessage = isApiGateway
            ? MessageBuilder.withPayload(payload).setHeader(AWSLambdaUtils.AWS_API_GATEWAY, true).build()

But, if it falls into the AWSLambdaUtils, it looks like it copies in the provided headers.

Object providedHeaders = ((Map) request).remove("headers");
            if (providedHeaders != null && providedHeaders instanceof Map) {
                messageBuilder.removeHeader("headers");
                messageBuilder.copyHeaders((Map<String, Object>) providedHeaders);
            }

How does one get the headers from FunctionInvoker or from the AWS adapter without using deprecated classes?

Is it intentional to not provide functions access to request headers?


Solution

  • This seems ugly but it works based on whats available.

    @Bean
    Function<Message<?>, Message<?>> test() {
        return msg -> {
            Map<String, Object> headers = new HashMap<>(0);
            if (msg.getPayload() instanceof Map) {
                headers = (Map<String, Object>)msg.getPayload();
                headers.forEach((key, value) -> log.error("Key: " + key + " Value: " + value));
            } else {
                log.error(new String((byte[]) msg.getPayload(), StandardCharsets.UTF_8));
            }
    
            return msg;
        };
    }
    

    The byte[] check is for passing the lambda test through console.

    Hoping someone provides a better answer..