Omi Harjani
Omi Harjani

Reputation: 840

decode method returns unicode response in lambda env

Decode function in Python 3.7 environment in aws lambda returns 1\u0000\u0000\u0000\u000001 at [1] while in local python 3.7.2 interpretter it returns 101

def lambda_handler(event, context):

    data = b'1\x00\x00\x00\x0001'
    response = data.decode()
    print(response)#[1]
    return {
        'statusCode': 200,
        'body': str(response)
    }

while local interpretter,

>>> data = b'1\x00\x00\x00\x0001'
>>> print (data.decode())
101

I require 101 as the response from the lambda 3.7 interpreter as well. Any suggestions are welcome.

Upvotes: 2

Views: 1217

Answers (1)

Mark Cinco
Mark Cinco

Reputation: 192

Just remove the null values (\x00) in the string. Print won't be able to output correctly if you have those null values in your string.

data.decode('utf8').replace('\x00', '')

Upvotes: 2

Related Questions