Testing from ApiGateay works, but CLI invoke fails on event data parse

aws

#1

using this https://github.com/pmuens/serverless-crud after installing it, I am able to run a test from the api gateway console with no problems. I use this event data:
{“id”:“334”,“note”:“this is another note here.”}

However with the same event data in the event.json file and calling invoke from cli like this:
serverless invoke --function create -p event.json
I get an error: 2016-10-14T21:03:32.404Z aa04dba8-9251-11e6-9d7a-4d5ce2f4cb21 SyntaxError: Unexpected token u
at Object.parse (native)

what i notice is it converts the event payload to use single quote instead of double, which i think is why it errrors out:
{ id: ‘335’, note: ‘this is another note here.’ }


#2

Hey @Jeff yes, that’s the reason.

Because it uses HTTP endpoints (with the new lambda-proxy integration type) we need to parse the payload in order to process it.


#3

U mean I need to do something in my code ?


#4

Hey @Jeff

The code in the service is written in a way that it can be used right away (the body of the response needs to be parsed as it’s stringified beforehand --> https://github.com/pmuens/serverless-crud/blob/master/todos-create.js#L8).

It needs to be updated only when you want to invoke the function from the console.


#5

I am encountering this now. I have set up a basic RESTful set of services. Everything works fine, and I can use Postman, curl, etc. to execute through the API Gateway. But I would like to invoke my lambda functions from the CLI directly. The problem, as mentioned here, is that I am trying to parse already parsed JSON (e.g. JSON.parse(event.body).

Is there a pattern that is being followed to address this?


#6

Is there a common pattern to deal with this issue now?


#7

@bush if you invoke your Lambda from directly from the CLI then you need to pass in an event object that looks like the one the API Gateway would pass. This means the body needs to be a string containing your JSON object. In simple terms body = JSON.stringify(YOUR-JSON-OBJECT).