Could not parse request body into json: Unexpected character (\'-\' (code 45)) AWS Lambda + API + Postman

前端 未结 3 515
说谎
说谎 2021-01-03 23:48

I have been trying for a few days to get a parameter sent from the API Gateway in AWS to a Lambda function and I am having no success.

I decided to start from the b

相关标签:
3条回答
  • 2021-01-04 00:38

    I just got stuck with this today.

    your mapping template is:

    { "name": "$input.params('name')" }
    

    AWS uses AWS Velocity templates, which; even though looks like JSON, is different.

    if you use

    { "name": $input.params('name') } // notice no quotes
    

    for the mapping template right at the integration request step, then it should work as expected.

    0 讨论(0)
  • 2021-01-04 00:38

    Read the error message very carefully, it actually tells you the problem. For example, I got

    Could not parse request body into json: Unexpected character (\'\"\' (code 34)): was expecting comma to separate Object entries

    So the problem is that I'm missing a comma. I check my Lambda's Integration Request - Body Mapping Template:

    {
    "age" : $input.json('$.persondata.age'),
    "income" : $input.json('$.persondata.income')
    "height" : $input.json('$.persondata.height')
    }
    

    Can you spot the problem? I am missing a comma after the income line.


    Here is another example.

    Could not parse request body into json: Unexpected character (\'}\' (code 125)): expected a value

    When I look at the Integration Request - Body Mapping Template:

    #set($inputRoot = $input.path('$'))
    {
      "age" : $inputRoot.age,
      "height" : $inputRoot.height,
      "income" : $inputRootincome
    }
    

    Can you spot the problem? I am missing a dot in $inputRootincome.

    0 讨论(0)
  • 2021-01-04 00:45

    Error Message :

    Could not parse request body into json: Could not parse payload into json: Unrecognized token \' \': was expecting (\'true\', \'false\' or \'null\')
    

    Cause of the error : When string values inside the json are not assigned using double quotations in the aws mapping template the error occurs.

    Solution : (Please Note : This example is for application/json type request template)

    Actually the solution for the problem is, if you are using a value of type string in json then its value should be assigned inside a ("" - double quotation marks) in the mapping template.

    The below shown example has the following attributes :

    customerId - string (Please note : this value comes from a query parameter)
    customerName - string
    customerAge - integer
    isPermanentEmployee - boolean
    customerAddress - string (Please note this is an optional parameter)
    

    And the mapping template should be defined like the example shown below

    Refer the example below :

    #set($inputRoot = $input.path('$'))
    {
      "CustomerId": "$input.params('customerId')",
      "CustomerName": "$inputRoot.customerName",
      "CustomerAge": $inputRoot.customerAge,
      "IsPermanentEmployee": $inputRoot.isPermanentEmployee
      "CustomerAddress ": #if($inputRoot.customerAddress == "") "" #elseif($inputRoot.customerAddress != "") "$inputRoot.customerAddress" #end
    }
    

    If you note the above mapping template, I would have given string values inside double quotation marks("") which will solve the error

    Also this example contains how to handle optional parameters in aws mapping templates using #if#else statements.

    0 讨论(0)
提交回复
热议问题