0

I have a Lambda@Edge function that will fire when a user connects to my website to perform authentication, which is controlled by a CloudFront distribution I've setup for my website.

I want to develop this code locally so I setup a dummy HelloWorld project using AWS SAM CLI and replaced its source code with my lambda@edge function. I've also created a JSON file that represents the event that will be passed to my lambda function during sam local invocation or when I run tests, placed under the "event" directory under my project folder.

I am able to run unit tests via npm run test and have it both fail and pass so I know the code base is working. However, when I try to invoke it using sam local invoke -e events/sample_event.json, I get the following error:

UnicodeEncodeError: 'latin-1' codec can't encode character '\ufeff' in position 0: Body ('\ufeff') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.

The traceback shows this error was emitted from a Python 3 HTTP client: /usr/local/Cellar/python@3.8/3.8.6_2/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py

Does anyone know why this encoding error is being emitted? I've used converters to ensure the contents of the event JSON file conform to utf-8 format. I can't understand why this is happening and what I should do to remedy it. Any suggestions are welcomed.

Alex
  • 1,293
  • 1
  • 13
  • 26
  • Does this answer your question? [UnicodeEncodeError: 'latin-1' codec can't encode character](https://stackoverflow.com/questions/3942888/unicodeencodeerror-latin-1-codec-cant-encode-character) (you may have bad quotation marks) – petey Dec 03 '20 at 19:54
  • You are right I am using double quotes. However, this is done because JSON uses double quotes for key and string values. So I don't think I can switch it to single quotes. I got it working by creating a completely new SAM project and using the "hello world" template then modifying its contents as it comes with an example event JSON file. – Alex Dec 03 '20 at 21:30
  • 1
    So yea, this question is likely a dupe. However, change your **Right & left double quotation marks** to **neutral quotation marks** and you will be fine. more info here: https://www.cl.cam.ac.uk/~mgk25/ucs/quotes.html – petey Dec 04 '20 at 06:49

0 Answers0