Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda.Invoke failing with payload; "Could not parse request body into json" #1876

Closed
brettneese opened this issue Jan 12, 2018 · 12 comments
Closed
Labels
closed-for-staleness documentation This is a problem with documentation.

Comments

@brettneese
Copy link

brettneese commented Jan 12, 2018

Using 2.180.0, running simply:

var AWS = require('aws-sdk');
var lambda = new AWS.Lambda();

var params = {
  FunctionName: 'hello-world' /* required */,
  Payload: 'STRING_VALUE' /* Strings will be Base-64 encoded on your behalf */
};

lambda.invoke(params, function(err, data){
  if(err){console.log(err, err.stack);} // an error occurred
  else {console.log(data);} // successful response
});

as per the SDK docs returns:

"{ InvalidRequestContentException: Could not parse request body into json: Unrecognized token 'STRING_VALUE': was expecting ('true', 'false' or 'null')}"

This seems to be a response from the Lambda server, so either the docs are wrong or Lambda is broken. I don't know where else to submit this bug, since AWS support is impossible to get a hold of in a timely fashion without paying AWS lots of money.

Setting the Payload to new Buffer('hello world') also fails.

Running it without a Payload is fine, although rather useless. I should have more than adequate permissions on my local machine to execute the function.

The full stack trace:

 at [Source: [B@51e2db23; line: 1, column: 25]
    at Object.extractError (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/protocol/json.js:48:27)
    at Request.extractError (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/protocol/rest_json.js:52:8)
    at Request.callListeners (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    at Request.emit (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
    at Request.emit (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/request.js:685:12)
  message: 'Could not parse request body into json: Unrecognized token \'STRING_VALUE\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: [B@51e2db23; line: 1, column: 25]',
  code: 'InvalidRequestContentException',
  time: 2018-01-12T16:57:38.437Z,
  requestId: 'b1e6c1b5-f7b9-11e7-88ec-77d8712e737a',
  statusCode: 400,
  retryable: false,
  retryDelay: 40.419110106244524 } 'InvalidRequestContentException: Could not parse request body into json: Unrecognized token \'STRING_VALUE\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: [B@51e2db23; line: 1, column: 25]\n    at Object.extractError (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/protocol/json.js:48:27)\n    at Request.extractError (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/protocol/rest_json.js:52:8)\n    at Request.callListeners (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/sequential_executor.js:105:20)\n    at Request.emit (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/sequential_executor.js:77:10)\n    at Request.emit (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/request.js:683:14)\n    at Request.transition (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/request.js:22:10)\n    at AcceptorStateMachine.runTo (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/state_machine.js:14:12)\n    at /Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/state_machine.js:26:10\n    at Request.<anonymous> (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/request.js:38:9)\n    at Request.<anonymous> (/Users/brettneese/Development/git/github.com/HBKEngineering/chartroom-api/node_modules/aws-sdk/lib/request.js:685:12)'
@brettneese
Copy link
Author

For future reference, I was able to do:

var object = {hello: "world"}

var params = {
  FunctionName: 'hello-world' /* required */,
  Payload: JSON.stringify(object)' /* Strings will be Base-64 encoded on your behalf */
};

However, I still contend that this means the docs are wrong, because they suggest you can send a string. Technically that's correct, but the string must be JSON.

@jeskew jeskew added the documentation This is a problem with documentation. label Jan 12, 2018
@jeskew
Copy link
Contributor

jeskew commented Jan 12, 2018

Hi @brettneese,

Unfortunately, the generated example code example referred to only knows about the data types required for each property of an operation's input and output; it is not aware of any semantic restrictions on what data those properties may contain.

Each property is documented below the example, however, which is where more thorough information about properties is communicated. The Payload property is documented as "JSON that you want to provide to your Lambda function as input."; it might be helpful to link directly to this documentation from the property name in the generated example.

@brettneese
Copy link
Author

Hey @jeskew, yeah, that makes sense. I read this thing many times and did not see that, and it seems there are a few others on the internets with this problem without a solution as well (hopefully they will Google and find this answer). The message coming back from Lambda is also less than helpful here, so that might be a good solution.

Thanks for your response!

@10001oleg
Copy link

I have same bug

Function Logs: START RequestId: 99c5b8a1-1493-4207-b931-0a0a87539064 Version: $LATEST 2019-05-01T21:33:46.346Z 99c5b8a1-1493-4207-b931-0a0a87539064 { InvalidRequestContentException: Could not parse request body into json: Unrecognized token 'STRING_VALUE': was expecting ('true', 'false' or 'null') at [Source: (byte[])"STRING_VALUE"; line: 1, column: 25] at Object.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:48:27) at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/rest_json.js:52:8) at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20) at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10) at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14) at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10) at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12) at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10 at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9) at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12) message: 'Could not parse request body into json: Unrecognized token \'STRING_VALUE\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: (byte[])"STRING_VALUE"; line: 1, column: 25]', code: 'InvalidRequestContentException', time: 2019-05-01T21:33:46.289Z, requestId: '9782dd23-405e-4ad6-9f80-0229d548250e', statusCode: 400, retryable: false, retryDelay: 22.979289126003046 } 'InvalidRequestContentException: Could not parse request body into json: Unrecognized token \'STRING_VALUE\': was expecting (\'true\', \'false\' or \'null\')\n at [Source: (byte[])"STRING_VALUE"; line: 1, column: 25]\n at Object.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:48:27)\n at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/rest_json.js:52:8)\n at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20)\n at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10)\n at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)\n at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)\n at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)\n at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10\n at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)\n at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)' END RequestId: 99c5b8a1-1493-4207-b931-0a0a87539064

//////MY CODE
// ASYNC SYNC var params = { FunctionName: 'arn:aws:lambda:us-west-2:*******************' /* required */, InvokeArgs: // new Buffer('...') || 'STRING_VALUE' || streamObject /* required */, 'STRING_VALUE', }; lambda.invokeAsync(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); // ASYNC SYNC

@10001oleg
Copy link

10001oleg commented May 1, 2019

I GOT IT!

    FunctionName:
      'arn:aws:lambda:us-west-2:372751*****:function:imageGen' /* required */,
    InvokeArgs:
      // new Buffer('...') || 'STRING_VALUE' || streamObject /* required */,
      JSON.stringify('STRING_VALUE'),
      // '"STRING_VALUE"', //if string needed - you can use this 
  };
  lambda.invokeAsync(params, function(err, data) {
    if (err) console.log(err, err.stack);
    // an error occurred
    else console.log(data); // successful response
  });
  // ASYNC SYNC
};```

@github-actions
Copy link

Greetings! We’re closing this issue because it has been open a long time and hasn’t been updated in a while and may not be getting the attention it deserves. We encourage you to check if this is still an issue in the latest release and if you find that this is still a problem, please feel free to comment or open a new issue.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Dec 15, 2020
@dcrearer
Copy link

dcrearer commented Jun 5, 2021

aws lambda invoke --function-name demo-func-async --invocation-type Event --payload $(echo '{ "name": "crearerd" }' | base64) response.json

encoding the payload to base64 works for me

@kumarAYUSH08
Copy link

Hello guys, I am also trying to do something similar...and facing error.
I need to parse json through s3. So I'm passing the ARN as payload while calling the invoke function and facing issues.
Below is the code file.
invoke_lambda.txt

@danchaseCTS
Copy link

danchaseCTS commented Sep 20, 2021

I've just blown the last 3 hours because I couldn't get the payload to make it into the lambda, and following Amazon's example exactly, did not work. Where does it say that it has to be base64 encoded? Once I tried to do this using Invoke (payload) in C#, the client immediately complained it could not encode JSON.. So I just created a request object and serialized it directly into the payload variable, and it finally worked. Hope this helps someone. Edit: I had tried to create the string manually and it wasn't having it, the member variable in the object came through as null, no errors.

@JacobWeyer
Copy link

Yeah explicitly in the CLI docs here: https://docs.aws.amazon.com/cli/latest/reference/lambda/invoke.html#examples

aws lambda invoke \
    --function-name my-function \
    --payload '{ "name": "Bob" }' \
    response.json

Yet if I do the same thing I get a "Could not parse request body into json" from the cli.

@georgeha
Copy link

@joematune
Copy link

@JacobWeyer - you'll need to add the following flag, --cli-binary-format raw-in-base64-out ~> docs link

aws lambda invoke \
  --function-name my-function \
  --payload '{ "name": "Bob" }' \
  --cli-binary-format raw-in-base64-out \
  response.json

# or from a file, you can use `fileb`
aws lambda invoke \
  --function-name my-function \
  --payload fileb://payload.json \
  response.son

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-for-staleness documentation This is a problem with documentation.
Projects
None yet
Development

No branches or pull requests

9 participants