我已经使用Swagger 2.0定义创建了API网关:
swagger: "2.0"
info:
description: "Everything to do with Client Management"
version: "1.0.0"
title: "Client Management API"
basePath: "/client"
schemes:
- "https"
paths:
/:
post:
summary: "Create a new Client"
description: "Adds a new Client to the system"
operationId: "addClient"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- in: "body"
name: "Client"
description: "Client object that needs to be added to the system"
required: true
schema:
$ref: "#/definitions/Client"
responses:
200:
description: "Client created successfully"
schema:
$ref: "#/definitions/Client"
400:
description: "invalid client object passed"
x-amazon-apigateway-integration:
type: "aws"
credentials: "arn:aws:iam::<account_id>:role/client-management/api-gateway-lambda-execution"
uri: "arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:<account_id>:function:client-management_create-client:$LATEST/invocations"
httpMethod: "POST"
responses:
default:
statusCode: "400"
200:
statusCode: "200"
schema:
$ref: "#/defintions/Client"
definitions:
Client:
type: "object"
properties:
name:
type: string
required:
- name
与该资源集成在一起的lambda函数是使用Python3编写的,如下所示(我遵循了本教程here):
import boto3
import json
def lambda_handler(event, context):
clientResponse = {
"name": event['name']
}
return {
"statusCode": 200,
"headers": {
"Content-Type" : "application/json"
},
"body": clientResponse
};
当我测试API资源时,我输入以下测试数据:
{
"name": "Test Client One"
}
但是API网关日志显示以下内容,这确实让我感到困惑!
请求:/
状态:400
延迟:106毫秒
响应主体
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"name": "Test Client One"
}
}
日志(我仅包括收到集成响应时的日志,以节省空间)
Tue Aug 14 19:08:40 UTC 2018 : Received response. Integration latency: 68 ms
Tue Aug 14 19:08:40 UTC 2018 : Endpoint response body before transformations: {"statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": {"name": "Test Client One"}}
Tue Aug 14 19:08:40 UTC 2018 : Endpoint response headers: {X-Amz-Executed-Version=$LATEST, x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x-amzn-RequestId=74b590e5-9ff5-11e8-9d49-4d0a8dca9cb9, Content-Length=105, Date=Tue, 14 Aug 2018 19:08:40 GMT, X-Amzn-Trace-Id=root=1-5b7328b8-ed37e03d28375bef4aa1c499;sampled=0, Content-Type=application/json}
Tue Aug 14 19:08:40 UTC 2018 : Method response body after transformations: {"statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": {"name": "Test Client One"}}
Tue Aug 14 19:08:40 UTC 2018 : Method response headers: {X-Amzn-Trace-Id=Root=1-5b7328b8-ed37e03d28375bef4aa1c499;Sampled=0, Content-Type=application/json}
Tue Aug 14 19:08:40 UTC 2018 : Successfully completed execution
Tue Aug 14 19:08:40 UTC 2018 : Method completed with status: 400
让我感到困惑的是,该方法成功完成,但是它的HTTP状态码为400 ...
有人能向我解释为什么会这样吗?
到目前为止,我最好的猜测是这是因为我已将默认响应(在Swagger 2.0定义中)设置为400。但是,我希望它返回200状态代码,该代码可以在响应正文中看到。
几天来,我一直在努力与Google进行交流,以寻求帮助,因此我们将不胜感激
谢谢