1

I working on an AWS Lambda function in python, sample below, and hooked up to an API Gateway via lambda_proxy integration. I have a post method in my api, which posts a json object in the request body as such:

{'request' : 'here are the details'}

In the lambda function I read the payload as such. This works but when there is a newline character in the request string, I get a json decode error. How to fix this?

import json
def lambda_handler(event, context):

   #read body
   body = json.loads(event['body'])
   #print body
   ...

i did the json.dumps(event) , output below

{
   "resource": "/myresource",
   "path": "/myresource",
   "httpMethod": "POST",
   "headers": {
      ...
    }, 
    ...
    "body": "{\"request\": \"here are the details \nhere some other details\"}",
"isBase64Encoded": false
 }
....
}
kyagu
  • 155
  • 2
  • 11
  • 1
    What is the full error message? What exactly is `event`? – Marcin Oct 05 '22 at 02:03
  • @kyagu if the json object you put it the question is the result of `json.dumps(event)` then you should probably do `json.loads(event)` instead of `event['body']`. – ganjim Oct 05 '22 at 02:27
  • @MoGanji - thanks , i just updated the question with json.dumps(event) – kyagu Oct 05 '22 at 02:31
  • 1
    Why do you put `\n` into the request in the first place? – Marcin Oct 05 '22 at 02:37
  • Does this answer your question? [Python json.loads fails with \`ValueError: Invalid control character at: line 1 column 33 (char 33)\`](https://stackoverflow.com/questions/9295439/python-json-loads-fails-with-valueerror-invalid-control-character-at-line-1-c) – ganjim Oct 05 '22 at 02:40

0 Answers0