aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

fix: sam local start api reques to correclty support binary files in request

Open vicheey opened this issue 7 months ago • 4 comments

Which issue(s) does this change fix?

https://github.com/aws/aws-sam-cli/issues/7927

Why is this change necessary?

From original issue:

When sending file (postman or frontend) to backend lambda through API Gateway I'm getting an error while doing local testing with sam local start-api

UnicodeDecodeError while processing HTTP request: 'utf-8' codec can't decode byte 0xb5 in position 473: invalid start byte.

How does it address the issue?

Update logic in event_constructor.py to decode request for various cases. .

What side effects does this change have?

  • This change is mostly additive and should not break existing functionality
  • It may change behavior for edge cases, but in ways that align better with API Gateway's actual behavior
  • The core logic for common cases remains the same

Mandatory Checklist

PRs will only be reviewed after checklist is complete

  • [ ] Add input/output type hints to new functions/methods
  • [ ] Write design document if needed (Do I need to write a design document?)
  • [X] Write/update unit tests
  • [ ] Write/update integration tests
  • [ ] Write/update functional tests if needed
  • [X] make pr passes
  • [ ] make update-reproducible-reqs if dependencies were changed
  • [ ] Write documentation

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

vicheey avatar May 12 '25 22:05 vicheey

Want for https://github.com/aws/aws-sam-cli/pull/8019 to merge. This contains some fix for failing makr pr test.

vicheey avatar May 13 '25 17:05 vicheey

Waiting for https://github.com/aws/aws-sam-cli/pull/8022 to merge before rerun the workflow.

vicheey avatar May 19 '25 21:05 vicheey

I'm curious how much manual testing you did here, because the unit tests seem nice, but I don't really know if there are cases where the API request actually looks different than the mocked requests data that you added. Did you try this with curl/wget or another mechanism to actually send a binary file and confirmed that the file was received correctly? And tried with non-binary files, to confirm that we didn't break any of those?

Ideally you can run the local start-api integration tests in tests/integration/local/start_api/test_start_api.py to confirm that nothing broke.

But ideally we should have had a test before that should have failed because this behavior wasn't working. We do have some integ tests that check with a binary file, so it's weird that no test was catching this problem. https://github.com/aws/aws-sam-cli/blob/8473d598a88ee5e309cb199fa98f7a09560d9491/tests/integration/local/start_api/test_start_api.py#L622

valerena avatar May 20 '25 23:05 valerena

overall looks good, but can we keep the original test while adding new ones?

roger-zhangg avatar Oct 07 '25 22:10 roger-zhangg