-1

I have created an API with AWS Lambda in Python. Unfortunately, the response contains the headers, and I would like it to only contain the body. The Lambda API looks like this:

import json
import boto3

def lambda_handler(event, context):
    
    #Step1: Scan table
    client = boto3.resource('dynamodb') #Access DynamoDB
    table = client.Table("register-to-event") #Access table
    
    
    response = table.scan()

    return {
        "statusCode": 200,
        "headers": {"Content-Type": "application/json",
        },
        "body": response["Items"]
    }

The problem is that the API response contains headers and body when I call it. This is the response:

{
  "statusCode": 200,
  "headers": {
    "Content-Type": "application/json"
  },
  "body": [
    {
      "your-email": "hannes.hannesen@googlemail.com",
      "your-number": "004785454548",
      "your-last-name": "Hannesen",
      "your-first-name": "Hannes",
      "ID": 3
    },
    {
      "your-email": "stig.stigesen@googlemail.com",
      "your-number": "+4754875456",
      "your-last-name": "Stigesen",
      "your-first-name": "Stig",
      "ID": 0
    }
  ]
}

The goal is to call the API and return only the body which is json like this:

[
    {
      "your-email": "hannes.hannesen@googlemail.com",
      "your-number": "004785454548",
      "your-last-name": "Hannesen",
      "your-first-name": "Hannes",
      "ID": 3
    },
    {
      "your-email": "stig.stigesen@googlemail.com",
      "your-number": "+4754875456",
      "your-last-name": "Stigesen",
      "your-first-name": "Stig",
      "ID": 0
    }
  ]
thenarfer
  • 405
  • 2
  • 14
  • Why do you want to suppress headers? – jarmod Jul 22 '21 at 18:04
  • I (think I) understand why you ask this, the problem is that I see these headers when following the API link. I wish to use the json-data to create a table with wpTables in Wordpress. This extension wants to see only the body, and not the headers. I hope you understand :-) – thenarfer Jul 22 '21 at 18:07
  • I'm doing this for the first time, so there might be some gaps in my understanding. I do know that opening a typical API in the browser will not show the headers in the browser window, only in "inspect element". In my case, they do show in the window. – thenarfer Jul 22 '21 at 18:18
  • Any SDK you use to make an HTTP API request will give you access to the status code, headers, and body independently. – jarmod Jul 22 '21 at 18:18
  • Typically you would return a dict with statusCode and optional headers, as you have already done, but the body would be `json.dumps(response["Items"])`. And you would have configured [API Gateway Lambda proxy integration](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html). – jarmod Jul 22 '21 at 18:32
  • `json.dumps(response["Items"])` gives `"Object of type Decimal is not JSON serializable"` – thenarfer Jul 22 '21 at 18:41
  • 1
    You can search for solutions for serializing decimals. In the meantime, why not set the body to `json.dumps({'name':'douglas', 'id':42})` i.e. a simple object and test that works (once you have proxy integration configured). – jarmod Jul 22 '21 at 18:55
  • Example of serializer: https://stackoverflow.com/questions/1960516/python-json-serialize-a-decimal-object – jarmod Jul 22 '21 at 19:24
  • That's a very good idea! Though, the resulting API call still contains the headers: `{"statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": "{\"name\": \"douglas\", \"id\": 42}"}` – thenarfer Jul 22 '21 at 19:36
  • I think I'm on to the answer now! In AWS there are set-up options for the API. I think if I can set up the correct methods, then it should work! I will try it and post an answer if I manage to get it working! Thank you so much for the help so far! – thenarfer Jul 22 '21 at 19:48

1 Answers1

0

The Solution was to configure the API in AWS correctly by ticking the box next to "Use Lambda Proxy integration": Use Lambda Proxy integration

My next problem ended up being "serializing decimals" as a result of DynamoDB making the keys of the table a type (Decimal) which does not exist in Python. The solution to this can be found here: https://learn-to-code.workshop.aws/persisting_data/dynamodb/step-3.html

thenarfer
  • 405
  • 2
  • 14
  • Hi, glad the problem is resolved. Going forward, it's generally considered good practice on Stack Overflow that if someone essentially provides the answers to you through back and forth in comments, that you ask them to write up their answer. – jarmod Jul 23 '21 at 13:57
  • @jarmod, I am terribly sorry! I did not see that you already (twice!) pointed me to the proxy integration! I don't know how I could overlook this!!! I understand fully what you mean now, and I am sorry for taking credit for your help! – thenarfer Jul 24 '21 at 07:34