The Developer Space

Developer's Cafe

  • Cloud
  • Database
  • Open Source
  • Programming
  • Web Dev
  • Mobile
  • Security
  • QuickRef
  • Home
  • Cloud
  • How to fix “‘StreamingBody’ object is not subscriptable” in AWS Lambda boto3

How to fix “‘StreamingBody’ object is not subscriptable” in AWS Lambda boto3

Shameel Ahmed - AWS, Cloud, Programming, Python
February 12, 2023February 12, 2023 No Comments
0 0
0 0
Read Time2 Minute, 56 Second

The boto3 framework provides an easy-to-use programmatic interface to work with AWS Services and makes it easy to work with the JSON responses these services return. But it can get quite quirky when working with large and complex JSON responses. Learn how to fix this specific issue while working with the boto3 framework.

Table of Contents

  • The Error
  • The Fix
  • Share

The Error

Let’s assume that you’re trying to invoke a Lambda function (Target) from another Lambda function (Caller) using boto3 client’s invoke method. Let’s assume that the Target function returns the following JSON response.

#Called function (Target)
return {
  'statusCode': 200,
  'body': {
    'message' : 'Hello World!'
  }
}

You try to access the returned response using this code:

#Calling function (Caller)
message = response['Payload']['body']['message']

and you encounter this error:

{
  "errorMessage": "'StreamingBody' object is not subscriptable",
  "errorType": "TypeError",
  "requestId": "1cba0dc1-1382-478d-9061-6a2959596a1b",
  "stackTrace": [
  ]
}

The Fix

If you need to parse and consume this JSON from the Caller function, you need to understand how the response is packaged and returned from the Target function by the boto3 framework. To understand how the response is returned, invoke the function and print the response object.

lambda_client = boto3.client('lambda')
response = lambda_client.invoke(
  FunctionName = 'arn:aws:lambda:us-east-1:000000000000:function:target_function',
  InvocationType = 'RequestResponse'
)

print(response['Payload'])

You’ll see something similar to this:

{'ResponseMetadata': {'RequestId': '8e719177-508b-4bbc-88fa-400691cebe68', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 12 Feb 2023 04:57:15 GMT', 'content-type': 'application/json', 'content-length': '55', 'connection': 'keep-alive', 'x-amzn-requestid': '8e719177-508b-4bbc-88fa-400691cebe68', 'x-amzn-remapped-content-length': '0', 'x-amz-executed-version': '$LATEST', 'x-amzn-trace-id': 'root=1-63e871ab-7737df4b541ec559279b5a5a;sampled=0'}, 'RetryAttempts': 0}, 'StatusCode': 200, 'ExecutedVersion': '$LATEST', 'Payload': <botocore.response.StreamingBody object at 0x7f0d77b82d30>}

Note how the Payload Element is returned as a StreamingBody object. You should parse this and convert the stream object to a JSON object before you can start parsing it using the subscript notation. Change the code to this:

response_json = json.load(response['Payload'])
message = response_json['body']['message']
#print(response_json)

You should now be able to successfully read and parse the response. If you wish to debug, uncomment line #3 to see the complete response as a JSON string. The json.load method converts the stream object to a JSON object and then you can read the elements inside the JSON object.

If you found this article helpful and it solved your problem, then please leave a comment. Also, do not forget to share this blog with friends and coworkers.

Share

Facebook
Twitter
LinkedIn
Email

Post navigation

How to invoke OpenAI APIs from AWS Lambda functions
How to build a REST API using Amazon API Gateway to invoke OpenAI APIs

Related Articles

Invoke OpenAI APIs from API Gateway

How to build a REST API using Amazon API Gateway to invoke OpenAI APIs

Shameel Ahmed
March 11, 2023March 11, 2023 No Comments
Invoke OpenAI APIs from Lambda

How to invoke OpenAI APIs from AWS Lambda functions

Shameel Ahmed
February 5, 2023February 18, 2023 14 Comments
ChatGPT landing page

Learn Python with ChatGPT

Shameel Ahmed
December 26, 2022December 26, 2022 7 Comments

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%
(Add your review)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Contents

  • The Error
  • The Fix
  • Share

Categories

.NET Architecture Artificial Intelligence ASP.NET AWS Azure Books C# Career Cloud CodeProject Conversational Bots Database Data Security Facade IDEs Java Mobile MongoDB MySQL Open Source Patterns PostgreSQL Programming Python Redis Security SQL Server Tools Uncategorized Web Development Windows Phone

Recent Posts

  • How to build a REST API using Amazon API Gateway to invoke OpenAI APIs March 11, 2023
  • How to fix “‘StreamingBody’ object is not subscriptable” in AWS Lambda boto3 February 12, 2023
  • How to invoke OpenAI APIs from AWS Lambda functions February 5, 2023
  • Developer to Architect Series (Red Hat Enable Architect) January 16, 2023
  • Can ChatGPT replace Google Search? January 11, 2023

Archives

  • March 2023 (1)
  • February 2023 (2)
  • January 2023 (2)
  • December 2022 (1)
  • October 2022 (1)
  • July 2022 (2)
  • February 2022 (1)
  • November 2021 (1)
  • July 2021 (1)
  • June 2021 (1)
  • September 2020 (1)
  • May 2020 (2)
  • April 2020 (1)
  • October 2019 (1)
  • September 2019 (4)
  • July 2019 (2)
  • May 2018 (1)
  • September 2017 (1)
  • April 2017 (1)
  • April 2014 (1)
  • August 2011 (1)
  • June 2009 (1)
Copyright 2022. The Developer Space | Theme: OMag by LilyTurf Themes
  • DbStudio
  • Re:Link
  • shameel.net
  • Privacy Policy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
SAVE & ACCEPT