Using SLS 1.0.0-beta.2 and serverless-offline 3.0.0, I can't seem to get my POST data from event.body, I just get {isOffline: true} .
POST data shows when function is deployed to AWS. Are there other settings that I may have missed?
Are you using serverless-offline from the v1 branch? If yes this is a bug.
I would Check the Yml file.
Serverless 1.0 is very sensitive to the construction of this file. Make sure you used the right amount of spacing, no tabs etc.
Yes, I'm using the v1 branch. I did a quick fix locally with 2 changes:
In offline-default.req.vm I added
{
"body": $input.json("$")
}
I understand this is just the default, but seems like it should have some structure in it to begin with? I later just added the full default velocity template from serverless.
Also, in index.js, the request.payload comes through as an object (I'm using PostMan to test locally), but the script tries to parse it because it detects the application/json content type.
I changed:
if (contentTypesThatRequirePayloadParsing.indexOf(contentType) !== -1) {
try {
request.payload = JSON.parse(request.payload);
} catch (err) {
request.payload = {};
}
}
to
if (contentTypesThatRequirePayloadParsing.indexOf(contentType) !== -1 && typeof request.payload === 'string') {
try {
request.payload = JSON.parse(request.payload);
} catch (err) {
request.payload = {};
}
}
I'm on it, thanks @5inline !
I had the same problem. I cleaned file offline-default.req.vm. I believed this file should be empty by default.
Because my content-type was application/json and the content was a json object.
Oh, forgot to say: v2.8.3 adds @5inline 's fix.
@rroqueCoddera thanks for the insight. Will adjust shortly.
I made another change that I forget to mention:
if (contentTypesThatRequirePayloadParsing.indexOf(contentType) <0) {
Because my object already is an object, I think parser should be executed when Content-type is diferent of application/json.
Confirming that 5inline's fix worked for me on the _v1 branch.
I was having the same issue with querystring params, and after much stumbling about, found this works for the offline-default.req.vm file:
#set($allParams = $input.params())
{
"body": $input.json("$"),
"query": {
#foreach($key in $allParams.querystring.keySet())
"$key" : "$input.params($key)"
#if($foreach.hasNext),#end
#end
}
}
@Bilal-S can you fix the v1 branch please?
I will add the same default template that core serverless framework is using. I left users to decide whether they wanted to use it with a sample file on how to do so. I will just implement it since it seems that is the more common use case. I will look at @rroqueCoddera code suggestion later today.
closing.
Most helpful comment
closing.