Error: Lambda function response malformed, please check API Gateway configuration

According to the official documentation, my JSON response should include a body, headers, and a status code, which it does. However, when testing in API Gateway, I am encountering issues with receiving a malformed response.

Below is the output of my method:

"{\"body\": 200, \"headers\": {\"Content-type\": \"application/json\"}, \"statusCode\": 200}"

def addnumbers(message, context):

    result = message['num1'] + 1
    print(result)
    resp = {
        "statusCode": 200,
         "body":  result,
        "headers": { "Content-type": "application/json"}
    }
    return (json.dumps(resp))

I have tried passing in num1=1, but it doesn't provide any useful error messages. Any guidance or assistance would be greatly appreciated.

Answer №1

Alright, get ready for a detailed response.

First things first, make sure you have proxy integration enabled on the resource in your API that you want to work with.

Next step is to navigate to your lambda function. Take note of how I was previously attempting to pass in num1 from the "Event" or message. This was causing some issues for me initially. It's important to remember that (you can't perform a GET request with a body). Instead, the input to the lambda should look something like this:

{ "queryStringParameters": { "input": "Whatever input you want the lambda to test" } }

Now that we have configured our test for the lambda, it's time to actually write the code for the lambda itself.

I included this code snippet within :

def lambda_handler(event, context):

number = "Hello, " + event['queryStringParameters']['input']
out = {}
out['statusCode'] = 200
out['body'] = number

return (out)

If you run the test now, everything should be running smoothly.

Head back to the API Gateway and in the "Query Strings" Section, enter input=randomname

You should see the output as 'Hello, randomname'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide on retrieving a JSON response from a servlet and displaying it on a JSP page using jQuery Ajax

I am currently working on retrieving a JSON response that has been established within a servlet to be displayed on a JSP page. JSP page: <script> $(document).ready(function(){ $("#submitBut").click(function(){ var formData=ge ...

Parsing JSON data with inconsistent structure

In the scenario presented below, there are 2 potential "unusual" occurrences: if the status is NOK, the data element will be completely omitted certain attributes of elements within list may be absent (for instance, in the following example, key2 is miss ...

Working with JSON data in javascript

While trying to parse JSON data from a server, I came across a strange issue. The data consists of rows of data - essentially a list of lists - and it looks something like this: [[1,2,3],[4,5,6],[7,8,9]] When viewing the JSON data in FF (using Firebug), ...

Generate a new JSON reply using the current response

I received a JSON response that contains values for week 1, week 2, week 3, and week 4 under the 'week' key, along with counts based on categories (meetingHash) and weeks. I attempted to merge this data using the .reduce method but without succes ...

Creating a schema for JSON within the controller component

I am trying to create a JSON structure similar to this one, but with multiple items: charge = { items: [{ name: "Product A", value: 1000, amount: 2 }] } I have a collection called @items that I want to include in the JSON using @items. ...

I am having difficulty extracting an image from the JSON data retrieved from the service and displaying it in the recycler view

This is my custom Adapter class where I am facing an issue with parsing images from JSON data retrieved from a service. CustomRecyclerViewAdapter.class public class CustomRecyclerViewAdapter extends RecyclerView.Adapter<CustomRecyclerViewAdapter.ViewH ...

Leveraging oembed for dynamic content integration with SoundCloud in JSON

I'm looking to retrieve the latest 10 tracks of a user using oembed with json. $.getJSON("http://soundcloud.com/oembed", {url: "https://soundcloud.com/aviciiofficial", format: "json"}, function(data) { console.log(data.html); }) The d ...

Deliver search findings that are determined by matching criteria, rather than by identification numbers

I am seeking to return a JSON match upon form submission, rather than simply searching for a specific ID. However, I am uncertain about how to structure this. I have the ability to search for the necessary match in a JavaScript document within one of my n ...

Passing a Value from Child to Parent Function in Meteor: A Complete Guide

I am trying to pass the value of a result from a child element to its parent element. Initially, I used Session.set and Session.get which worked fine but I realize that using Sessions globally is not considered good practice. So, I attempted to utilize rea ...

Using Retrofit 2 in Android to parse a JSON array containing numerous objects

In my project, I am utilizing retrofit 2 for networking tasks. Parsing json object responses from the server is working perfectly, but now I need to parse a json array that has the following structure: [ { "id_asset": 1, "id_category": ...

When an Ajax post request is made, the data being sent is appended to the JSON response

Having a dilemma with my ajax call: $.ajax({ url: '/assets/functions.php', type: 'POST', data: { "functionCall": "get-uploads", "type": type }, dataType: 'json', success: function (data ...

Display a JSON encoded array using Jquery

Within an ajax call, I have a single json encoded array set: $var = json_encode($_SESSION['pictures']); The json encoded array is stored in a variable called "array" When I try to display the contents of "array" using alert, I get this respons ...

Change a CSV string into a JSON array and assign it to a variable

I am working with JSON data that looks like this: [ { "Title": "PAGE A", "Users": "USRA" }, { "Title": "PAGE B", "Users": "USRA,USRB" } ] What is the most efficient method to convert the fields containing " ...

Converting JSON to TypeScript in an Angular project

Within my Angular project, I have an HTTP service that communicates with a web API to retrieve JSON data. However, there is a discrepancy in the naming convention between the key in the JSON response (e.g., "Property" in uppercase) and the corresponding pr ...

What is the most effective way to import a substantial static array in React for utilization in a select field?

The scenario is quite straightforward. I currently have a single array containing over 2500 strings of company names, saved locally in the project as a JSON file within a subdirectory under src. To access this data in my component, I am importing the JSON ...

Issue retrieving data from JSON in Android app

Every time I attempt to make a service call in order to extract data from a Google spreadsheet, the JSON response is null. Any thoughts on why this might be happening? Main Activity private static final String url = https://docs.google.com/spreadsheets/d ...

Automatically populating state and city fields with zip code information

Starting out in the world of web development, I encountered a challenge with a registration form I'm constructing for our company. For guidance, I referred to this resource: http://css-tricks.com/using-ziptastic/. This project marks my initial interac ...

Struggling to link AWS S3 ReactApp with Heroku Node/Express API

I successfully deployed a react-app to AWS S3 and a node/express API on Heroku, but I am facing difficulties in connecting them together even with the cors configuration in the API or using proxy in the react-app. I have been unable to resolve this issue. ...

Unraveling the complexities of parsing multi-tiered JSON structures

I am struggling with indexing values from a multi-level JSON array. Here is the JSON data in question: { "items": [ { "snippet": { "title": "YouTube Developers Live: Embedded Web Player Customization" } ...

Retrieving information from dictionary in swift version 4

Currently, I am utilizing an API and trying to display its data in a table view. import UIKit import Alamofire class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! @IBOu ...