Phillng
November 13, 2021, 7:22pm
1
I am calling an AWS Lambda function using API Gateway, which returns a simple response:
return {
'statusCode': 200,
'body': json.dumps({"hi":"hi"})
}
Adalo is able to call the endpoint and read the payload body, but says it is unsupported content.
I have tried various results key (i.e. “body”) and various Headers (i.e. “Content-Type”:“application/json” and “text/plain”. Nothing works. Would you have an idea?
TKOTC
November 13, 2021, 7:52pm
2
I am not at my computer at the moment, but I am pretty sure Adalo needs the response payload to be wrapped in results
So your lambda needs to return
{ "results": { "hi": "hi" } }
Give that a shot, if it doesn’t work let me know and when I get back to my computer I’ll double check and let you know.
2 Likes
Phillng
November 13, 2021, 8:37pm
3
Thanks for the reply! Unfortunately, I tried that and it gave me the same error:
Lambda Function:
return {
'statusCode': 200,
'body': json.dumps({"results":{"hi":"hi"}})
}
Adalo:
TKOTC
November 13, 2021, 9:21pm
4
My mistake, the external collection will run a Get All (it is a collection after all). It must return an array, not an object! Normally structured with “response”, so return
{
"response": {
"data": [{
"hi": "hi"
}]
}
}
try this
return {
'statusCode': 200,
'body': json.dumps({ "response": { "data": [{"hi":"hi"}] } })
}
Notice that “data” is an array. If you want to not return an array then you do not use external collection, but a custom action that calls an endpoint.
1 Like
Phillng
November 13, 2021, 9:49pm
5
Thanks for the reply again @TKOTC . We’ll be doing both a collection (with an array) and a custom action. I did as you instructed and tried the code you suggested:
return {
‘statusCode’: 200,
‘body’: json.dumps({ “response”: { “data”: [{“hi”:“hi”}] } })
}
Adalo:
However, it seems the issue persists even if “data” is an array. Any insights as to why? Thanks!
Phillng
November 13, 2021, 9:51pm
6
Also, I did input the “results key” and “second results key”, using combinations of “response” and “data” for each field.
TKOTC
November 13, 2021, 9:53pm
7
Did you test it without putting any keys?
Phillng
November 13, 2021, 11:04pm
9
Still not working. I re-did the Endpoint (with no results key or headers), and the same issue persisted.
return {
'statusCode': 200,
'body': json.dumps({ "response": { "data": [{"hi":"hi"},{"there":"there"}] } })
}
TKOTC
November 13, 2021, 11:41pm
10
return {
"statusCode": 200,
"body": json.dumps({ "response": { "data": [{"hi":"hi"}] } }),
"content-type": "application/json; charset=UTF-8"
}
Phillng
November 13, 2021, 11:54pm
11
return {
'statusCode': 200,
'body': json.dumps({ "response": { "data": [{"hi":"hi"},{"there":"there"},{"again":"again"}] } }),
"content-type": "application/json; charset=UTF-8"
}
TKOTC
November 14, 2021, 12:05am
12
Can you share the yaml for the lambda? Something seems off with your lambda.
Phillng
November 14, 2021, 12:28am
13
So, I’m not sure what the .yaml file is for the lambda, I just created the lambda function from the dashboard. Here’s the code:
import json
import logging
import io
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps({ "response": { "data": [{"hi":"hi"},{"there":"there"},{"again":"again"}] } }),
"content-type": "application/json; charset=UTF-8"
}
And the Resource-Based Policy Document (with API Gateway invoking the function):
{
"Version": "2012-10-17",
"Id": "default",
"Statement": [
{
"Sid": "6f25300c-f555-50dc-9f8e-d95aee0e29ba",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "lambda:InvokeFunction",
"Resource": "arn:aws:lambda:us-east-2:406273866153:function:recommender",
"Condition": {
"ArnLike": {
"AWS:SourceArn": "arn:aws:execute-api:us-east-2:406273866153:6qmsb4au8l/*/*/recommender"
}
}
}
]
}
TKOTC
November 14, 2021, 1:25am
14
Ahhhh… I understand now!
Okay, here is the results
Here are the steps to make it work.
I used the following code in the lambda
import json
import logging
import io
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': [{"hi":"hi"},{"there":"there"},{"again":"again"}],
"content-type": "application/json; charset=UTF-8"
}
Notice, there is no json.dumps
Second, there issue if probably in your API Gateway! Not the lambda or Adalo.
Create a new API Gate and choose REST API (Not HTTP API, which is what I think you have done)
Leave the defaults and give it a name
Choose Create Method
Choose GET from the dropdown, click the check mark
Choose Lambda Function and enter the name of your lambda
Click OK to give permissions
Leave all the defaults, verify that the METHOD RESPONSE is “application/json”
Choose DEPLOY API from the ACTIONS dropdown
Choose NEW STAGE and give it information
Copy the INVOKE URL, this is what you use for Adalo
Edit the external collection
Enter the Result key as body
(from the lambda)
Test collection and click create
Let me know if this works out for you!
1 Like
TKOTC
November 14, 2021, 1:38am
15
Also, now that I am less confused, your Lambda should look like this
import json
import logging
import io
def lambda_handler(event, context):
return [{"hi":"hi"},{"there":"there"},{"again":"again"}]
No need for anything else. The return of a lambda_handler will get wrapped by the API Gateat so it will already include the status 200 (unless you throw an error) and the { “response”: { “data”: … } }
The above code will return
Which means you do not need to use the Results Key
for it to work
1 Like
Phillng
November 14, 2021, 4:35pm
16
Perfect, that worked just perfectly! I was able to return a list like you suggested.
2 Likes
system
Closed
November 24, 2021, 4:36pm
17
This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.