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

Request Template - Post JSON Body With Single Quote #187

Closed
dizard2001 opened this issue Jan 9, 2017 · 4 comments
Closed

Request Template - Post JSON Body With Single Quote #187

dizard2001 opened this issue Jan 9, 2017 · 4 comments

Comments

@dizard2001
Copy link

dizard2001 commented Jan 9, 2017

serverless-offline: 3.8.3
serverless: 1.4.0

When posting a Json object that contains a single quote in a property value, I was getting a bad request from the api-gateway. After researching I adjusted my code as per http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html to use $util.escapeJavaScript(data).replaceAll("\'","'"). I've used this and it's successful when the lambda function is deployed into AWS, but when running as serverless offline I get the following error:

Serverless: POST /feedback (λ: feedback)
Serverless: The first request might take a few extra seconds
Serverless:
Serverless: Error while parsing template "application/json" for feedback
[ 'Error: this.replace is not a function on $util.escapeJavaScript($input.json('$')).replaceAll("\\'","'") at L/N 2:11',
'at Velocity.utils.mixin.getPropMethod (/Users/danderson/src/project-task-board-api/node_modules/serverless-offline/node_modules/velocityjs/src/compile/references.js:297:19)',
...

My serverless.yml:
feedback: handler: functions/feedback/post.handler events: - http: cors: true path: feedback method: post integration: lambda request: template: application/json: > { "body": "$util.escapeJavaScript($input.json('$')).replaceAll("\\'","'")", "headers": { #foreach($param in $input.params().header.keySet()) "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end #end } }

I tried combing through both closed and open issues related to this and came across #50. If I'm understanding it correctly, the escape javascript call returns an object instead of a string(as in AWS) and so the function isn't defined. As per the recommendation of applying a JSON.stringify to the output, this is resulting in just an empty string. Any insight would be appreciated.

@dherault
Copy link
Owner

Hi @dizard2001 thanks for reporting this. It looks like a bug with the velocity parser helper functions. I believe it is trying to call this function but for some reason String#replace is not a function. It would be nice to know what is the type of the output of $util.escapeJavaScript(data) is.
The package we use to simulate $util.escapeJavaScript is js-string-escape as you can see here.

@dizard2001
Copy link
Author

dizard2001 commented Jan 12, 2017

@dherault sorry for the late reply. The type of the output of $util.escapeJavascript(data) is a String. Additionally the format of the return after that call in my case is:
{"comment":"test","like":true,"userEmail":"Administrator@test.com","userName":"Martin Scorsese","enterpriseId":"dd051afc-c95a-46af-a89a-7ec011e1f9b5","enterpriseName":"Development"}

When I have more time I can dig into the issue a bit more.

@dherault
Copy link
Owner

Hi guys, it is still the case in v3.11.0 ? Thanks

@dherault
Copy link
Owner

ping :) v3.13.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants