serverless-offline icon indicating copy to clipboard operation
serverless-offline copied to clipboard

Serverless 1.0 and event.body

Open 5inline opened this issue 10 years ago • 13 comments

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?

5inline avatar Aug 28 '16 22:08 5inline

Are you using serverless-offline from the v1 branch? If yes this is a bug.

dherault avatar Aug 29 '16 07:08 dherault

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.

Bilal-S avatar Aug 29 '16 13:08 Bilal-S

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 = {};
              }
            }

5inline avatar Aug 29 '16 13:08 5inline

I'm on it, thanks @5inline !

dherault avatar Aug 30 '16 07:08 dherault

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.

rroqueCoddera avatar Aug 30 '16 14:08 rroqueCoddera

Oh, forgot to say: v2.8.3 adds @5inline 's fix.

dherault avatar Aug 30 '16 14:08 dherault

@rroqueCoddera thanks for the insight. Will adjust shortly.

Bilal-S avatar Aug 30 '16 14:08 Bilal-S

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.

rroqueCoddera avatar Aug 30 '16 21:08 rroqueCoddera

Confirming that 5inline's fix worked for me on the _v1 branch.

Jorenm avatar Aug 30 '16 22:08 Jorenm

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
  }
}

Jorenm avatar Aug 31 '16 00:08 Jorenm

@Bilal-S can you fix the v1 branch please?

dherault avatar Aug 31 '16 07:08 dherault

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.

Bilal-S avatar Aug 31 '16 15:08 Bilal-S

closing.

Bilal-S avatar Sep 08 '16 19:09 Bilal-S