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

前端 未结 3 519
说谎
说谎 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: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.

提交回复
热议问题