Skip to content

feat(event_handler): Allow for cookies in response #1196

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

Conversation

michaelbrewer
Copy link
Contributor

@michaelbrewer michaelbrewer commented Apr 30, 2022

Issue number:

Summary

Currently there is no simple way to set cookies in the api gateway event handler.

And this is a gap for http api responses which supports a cookie field.

For documentation we may need to call out the following caveats:

  • multiValueHeaders is assumed for rest and alb, but only supported by ALB when enabled: ALB - Enable multi-value headers
  • cookies is assumed for http api but only supported by http api payload version 2

Changes

Please provide a summary of what's being changed

  • Add cookies field in Response
  • For http api gateway format 2.0 responses serialize to cookies
  • For alb and rest api use multiValueHeaders for cookies

User experience

Please share what the user experience looks like before and after this change

For http api handler

from aws_lambda_powertools.event_handler.api_gateway import APIGatewayHttpResolver, Response

app = APIGatewayHttpResolver()


@app.get("/my/path")
def my_path() -> Response:
    return Response(200, content_types.TEXT_PLAIN, "Foo", cookies=["key=value"])


def handler(event, context):
    return app.resolve(event, context)

Will output a http api payload v2 response:

{
    "statusCode": 200,
    "headers": {
        "Content-Type": "text/plain"
    },
    "body": "Test",
    "isBase64Encoded": false,
    "cookies": [
        "key=value"
    ]
}

For rest api handler

from aws_lambda_powertools.event_handler.api_gateway import APIGatewayHttpResolver, Response

app = APIGatewayRestResolver()


@app.get("/my/path")
def my_path() -> Response:
    return Response(200, content_types.TEXT_PLAIN, "Foo", cookies=["key=value"])


def handler(event, context):
    return app.resolve(event, context)

Outputs the following for ALB and Rest responses

{
    "statusCode": 200,
    "headers": {
        "Content-Type": "text/plain"
    },
    "body": "Test",
    "isBase64Encoded": false,
    "multiValueHeaders": {
        "Set-Cookie": [
            "key=value"
        ]
    }
}

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

Changes:
- Add cookies field in Response
- For http api gateway format 2.0 responses serialize to cookies
- For alb and rest api use multiValueHeaders

closes aws-powertools#1192
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 30, 2022
@github-actions github-actions bot added the feature New feature or functionality label Apr 30, 2022
@codecov-commenter
Copy link

codecov-commenter commented Apr 30, 2022

Codecov Report

Merging #1196 (e2723c3) into develop (9d8dd1b) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff            @@
##           develop    #1196   +/-   ##
========================================
  Coverage    99.88%   99.88%           
========================================
  Files          119      119           
  Lines         5423     5429    +6     
  Branches       618      620    +2     
========================================
+ Hits          5417     5423    +6     
  Misses           2        2           
  Partials         4        4           
Impacted Files Coverage Δ
aws_lambda_powertools/event_handler/api_gateway.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9d8dd1b...e2723c3. Read the comment docs.

@michaelbrewer michaelbrewer changed the title feat(event_handler): Allow cookies in response feat(event_handler): Add cookies in response Apr 30, 2022
@michaelbrewer michaelbrewer changed the title feat(event_handler): Add cookies in response feat(event_handler): Allow for cookies in response May 1, 2022
@sthulb
Copy link
Contributor

sthulb commented May 12, 2022

In an effort to prevent you from wasting time, you should wait for issues to prioritised, this won't be merged for a long time until we're ready to add new features.

@michaelbrewer
Copy link
Contributor Author

More of a gap than a feature. There is no easy way for customers to set cookies

@sthulb sthulb closed this Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants