Skip to content

Response headers not working in HttpAPI PayloadFormat V2 #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lbeuster opened this issue Aug 28, 2020 · 6 comments
Closed

Response headers not working in HttpAPI PayloadFormat V2 #377

lbeuster opened this issue Aug 28, 2020 · 6 comments
Assignees
Labels
Milestone

Comments

@lbeuster
Copy link

Currently it seems that the serverless container doesn't really support HttpApi with PayloadFormat V2 (with is the default in HttpApi). All headers (even with V2ProxyHandler) are written into the multiheader field but multiheaders are not supported anymore in V2: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html

I could reproduce this with a simple spring-boot2 lambda. And also with a simple non-serverless container lambda using the aws-lambda-java-events:APIGatewayV2HTTPResponse class (which still contains the multiheader field). Headers written to the header field were exposed, multiheaders not.

Best regards
Lars

@lbeuster lbeuster changed the title Support HttpAPI PayloadFormat V2 Headers not working in HttpAPI PayloadFormat V2 Aug 28, 2020
@sapessi
Copy link
Contributor

sapessi commented Aug 28, 2020

Hi @lbeuster - are you referring to the response headers? Our HTTP API request object does not have a multivalue headers field.

@lbeuster
Copy link
Author

Yes, the response headers.

@lbeuster lbeuster changed the title Headers not working in HttpAPI PayloadFormat V2 Response headers not working in HttpAPI PayloadFormat V2 Aug 28, 2020
@sapessi
Copy link
Contributor

sapessi commented Sep 10, 2020

I just checked with the API Gateway team. This is the answer:

  • If customer is using 1.0 of the lambda proxy format, api gw reads those on the response.
  • If customer is using 2.0 of the lambda proxy format, api gw does NOT read those on the response.

We'll mark this as a bug and address it with the next release

@sapessi sapessi self-assigned this Sep 10, 2020
@sapessi sapessi added the bug label Sep 10, 2020
@sapessi sapessi added this to the Release 1.6 milestone Sep 10, 2020
@masch712
Copy link

As a workaround, I created an anonymous ResponseWriter instance that concatenates the values in multiHeaderValues with their corresponding header value, if one exists. Here's how I build my lambda handler:

      handler = new SpringBootProxyHandlerBuilder<HttpApiV2ProxyRequest>()
          .springBootApplication(Blah.class)
          .defaultHttpApiV2Proxy()
          .responseWriter(
              new AwsProxyHttpServletResponseWriter() {
                @Override
                public AwsProxyResponse writeResponse(AwsHttpServletResponse containerResponse,
                    Context lambdaContext) throws InvalidResponseObjectException {
                  final AwsProxyResponse awsProxyResponse = super
                      .writeResponse(containerResponse, lambdaContext);
                  final Headers multiValueHeaders = awsProxyResponse.getMultiValueHeaders();
                  if (multiValueHeaders != null) {
                    Map<String, String> headers;
                    if (awsProxyResponse.getHeaders() == null) {
                      headers = new HashMap<>();
                    } else {
                      headers = awsProxyResponse.getHeaders();
                    }
                    multiValueHeaders.entrySet().forEach(entry -> {
                      headers.compute(entry.getKey(), (key, existingValue) ->
                      {
                        final String commaDelimitedMultiValueHeader = String
                            .join(",", entry.getValue());
                        return existingValue != null && !existingValue.isBlank()
                            ? (existingValue + "," + commaDelimitedMultiValueHeader)
                            : commaDelimitedMultiValueHeader;
                      });
                    });
                    awsProxyResponse.setMultiValueHeaders(null);
                    awsProxyResponse.setHeaders(headers);
                  }
                  return awsProxyResponse;
                }
              }
          )
          .buildAndInitialize();

@lbeuster
Copy link
Author

Thanks, but payload format version 1.0 is also a good workaround for the moment ;)

@sapessi sapessi modified the milestones: Release 1.6, 1.5.2 Oct 6, 2020
sapessi added a commit that referenced this issue Oct 6, 2020
Parametrized the use of the single value headers for the response writer to support the v2 proxy schema for HTTP API
sapessi added a commit that referenced this issue Oct 6, 2020
Changed default httpApiV2 constructor for the handler in all the framework implementations
@sapessi sapessi mentioned this issue Oct 6, 2020
2 tasks
sapessi added a commit that referenced this issue Oct 6, 2020
* fix: Use single value headers for HTTP API response (#377)

Parametrized the use of the single value headers for the response writer to support the v2 proxy schema for HTTP API

* fix: Updated HTTP API handler constructor (#377)

Changed default httpApiV2 constructor for the handler in all the framework implementations

* fix: Switched to non-mime encoder for respponse (#339)
@sapessi
Copy link
Contributor

sapessi commented Oct 6, 2020

1.5.2 has now hit maven central. Closing this issue.

@sapessi sapessi closed this as completed Oct 6, 2020
jogep pushed a commit to jogep/aws-serverless-java-container that referenced this issue Dec 8, 2020
Parametrized the use of the single value headers for the response writer to support the v2 proxy schema for HTTP API
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants