Vivek Sable
Vivek Sable

Reputation: 10223

How to pass bytes data in payload during invoking AWS Lambda function

I have on Lambda function which input is

input_str = "<html><body><h1>Title</h1><Hello World></body></html>"
input_base64 = base64.b64encode(bytes(input_str, 'utf-8'))
payload = {"html_base64" : input_base64} 

here input_base64 is bytes type variable

I am invoking this Lambda function from other Lambda function, but I am not able pass this payload

invoke_response = lambda_client.invoke(FunctionName="vivek_05",
                                       InvocationType='RequestResponse',
                                       Payload=payload
                                       )

Getting following exception:

  "errorMessage": "Parameter validation failed:\nInvalid type for parameter Payload, value: {'html_base64': b'PGh0bWw+PGJvZHk+PGgxPlRpdGxlPC9oMT48SGVsbG8gV29ybGQ+PC9ib2R5PjwvaHRtbD4='}, type: <class 'dict'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object",

Can you help me?

Upvotes: 3

Views: 7707

Answers (2)

IftekharDani
IftekharDani

Reputation: 3729

You can encode and decode bytes using this example. This might help you.

Encode by :

Payload=json.dumps({
            'imageName': image_name,
            'imageBytes': base64.b85encode(image_bytes).decode('utf-8'),
    })

You can decode by :

let imageBytes = base64.b85decode(event['imageBytes']);
let imageName = event['imageName'];

Upvotes: 4

Kannaiyan
Kannaiyan

Reputation: 13035

Since the data is a binary or bytes, you need to convert into string before sending it across.

input_base64str = str(input_base64,'utf-8')

Should fix it.

When you convert back, change it to bytes and pass it to base64 decoder.

input_base64 = bytes(input_base64str, 'utf-8')

Hope it helps.

Upvotes: 2

Related Questions