Reputation: 48
Iam trying to send a post request to aws lambda function throuth api gateway.
but when i send it by postman it return a error "Could not parse payload into json".
Unrecognized token
like the picture below(i remove some useless info):
next i try to send the same request through "Test Method" on the webpage of api gateway:
it successed! (iam pretty sure use the same request body)
extra info:
here is the code of my lambda:
def handle(event, context):
res = {'code': 200}
bad_res = {'code': 400}
if event['operation'] == 'JoinCluster':
# verify user by aws cognito
is_allowed, user = is_joining_allowed(event)
if is_allowed:
# add a item in dynamodb
client_info = client_table.add_requested_client(user, event["metadata"])
# get a available token from another table
token = token_table.get_valid_token()
res = {'owner': user, 'id': client_info['id'], **token, **res}
# Send a message to SQS to indicate that a node is ready to join
aws.send_message({'id': client_info['id']}, queue_name="netmind-node-join-queue")
return res
else:
bad_res["message"] = "You are not allowed to join this cluster, check your access token before retrying."
return bad_res
bad_res["message"] = "unknown operation."
return bad_res
Upvotes: 0
Views: 1892
Reputation: 137
I try your lambda with api-gateway from postman site, it works well.
Something you not mentioned above cause the problem.
In my postman page, Body tab with green dot at right hand. I also choose raw and JSON follow your screen capture.
But for Params and Authorization tab, I dont know what to set, and keep them without green lights. I guess something there make things wrong.
Following is my test api-gateway definition which works in my site.
You shoud change ARN_FOR_YOUR_LAMBDA
to your real lambda-arn before you can import into your api-gateway.
---
swagger: "2.0"
info:
description: "test"
version: "2022-04-02T15:18:02Z"
title: "PetStore"
host: "HOST_OF_YOUR_API"
basePath: "/test"
schemes:
- "https"
paths:
/mao:
post:
produces:
- "application/json"
responses:
"200":
description: "200 response"
schema:
$ref: "#/definitions/Empty"
x-amazon-apigateway-integration:
httpMethod: "POST"
uri: "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/ARN_FOR_YOUR_LAMBDA/invocations"
responses:
default:
statusCode: "200"
passthroughBehavior: "when_no_match"
contentHandling: "CONVERT_TO_TEXT"
type: "aws"
options:
consumes:
- "application/json"
produces:
- "application/json"
responses:
"200":
description: "200 response"
schema:
$ref: "#/definitions/Empty"
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: "string"
Access-Control-Allow-Headers:
type: "string"
x-amazon-apigateway-integration:
responses:
default:
statusCode: "200"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: "'*'"
requestTemplates:
application/json: "{\"statusCode\": 200}"
passthroughBehavior: "when_no_match"
type: "mock"
definitions:
Empty:
type: "object"
Wish this help.
Upvotes: 0