Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws cli lambda-Could not parse request body into json

I have created aws lambda function in .net core and deployed. I have tried executing function in aws console with test case and its working. but i am not able achieve the same with cli command

aws lambda invoke --function-name "mylambda" --log-type Tail --payload file://D:/Files/lamdainputfile.json file://D:/Files/response.txt

i got getting error with cli command

An error occurred (InvalidRequestContentException) when calling the Invoke operation: Could not parse request body into json: Unexpected character ((CTRL-CHAR, code 138)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: (byte[])"�zn�]t�zn�m�"; line: 1, column: 2]

I tried passing json

aws lambda invoke --function-name "mylambda" --log-type Tail --payload "{'input1':'100', 'input2':'200'}" file://D:/Files/response.txt

but it's not working

This lambda function is executing aws console with test case and giving correct result. I have added same input in local json file and tried with cli command.

Json input:

{
  "input1": "100",
  "input2": "200"
}

EDIT:

After correction in inline json i am getting error for output file

Unknown options: file://D:/Files/response.txt

is there any command to print output in cli only?

like image 928
sp_m Avatar asked Sep 30 '20 12:09

sp_m


2 Answers

The documentation is not updated from the cli version 1. For the aws cli version 2 we need to base64 encode the payload.

Mac:

payload=`echo '{"input1": 100, "input2": 200 }' | openssl base64`
aws lambda invoke --function-name myfunction --payload "$payload" SomeOutFile &
like image 103
user 923227 Avatar answered Sep 28 '22 18:09

user 923227


Adding the option --cli-binary-format raw-in-base64-out will allow you to pass raw json in the invoke command.

aws lambda invoke \
    --cli-binary-format raw-in-base64-out \
    --function-name "mylambda" \
    --payload '{"input1": "100", "input2": "200"}' \
    file://D:/Files/response.txt
like image 25
Jonathan Eckel Avatar answered Sep 28 '22 18:09

Jonathan Eckel