API网关:空请求正文?

时间:2017-10-14 10:29:45

标签: ios amazon-web-services post sdk aws-api-gateway

我正在尝试使用 AWS生成的 iOS(Swift)SDK AWS Lambda 函数发送 POST正文 API GATEWAY ,但传输的正文总是空的。

为此,我设置了一个模型:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "InputModel",
    "type": "object",
    "required" : ["name"],
    "properties": {
        "name": { "type": "string" },
    }
}

,我包含在 $ Resources - >中/ test - > POST - >方法执行 - >方法请求 - >请求正文面板。

然后我部署了API并下载并包含了API Gateway生成的SDK。现在我调用API(如Use Generated iOS SDK (Swift) to Call API中所推荐的那样),如下所示:

let apiClient = APITestClient.default()
let body = APITest()
body!.name = "HOLA"

apiClient.testPost(body: body!).continueWith { task -> Void in ... }

但我的Lambda函数收到的正文始终为空{},我在 API网关日志中检查了这些内容:

(c421a895-b0c7-11e7-89a6-891dcbadf1f0) Method request body before transformations:
{}

Lambda函数日志:

2017-10-14T10:09:29.418Z    c424b611-b0c7-11e7-99c1-251bdb6a1eac    Request:
{
    "resource": "/test",
    "path": "/test",
    "httpMethod": "POST",
    "headers": {
        "Accept": "application/json",
        "Accept-Encoding": "br, gzip, deflate",
        "Accept-Language": "en-us",
        "CloudFront-Forwarded-Proto": "https",
        "CloudFront-Is-Desktop-Viewer": "true",
        "CloudFront-Is-Mobile-Viewer": "false",
        "CloudFront-Is-SmartTV-Viewer": "false",
        "CloudFront-Is-Tablet-Viewer": "false",
        "CloudFront-Viewer-Country": "ES",
        "content-type": "application/json",
        "Host": "9hlt8n48wd.execute-api.eu-west-1.amazonaws.com",
        "User-Agent": "aws-sdk-iOS/2.6.4 iOS/11.0 en_US",
        "Via": "2.0 9ee2752d23ce7be13204dc3880c3ca6b.cloudfront.net (CloudFront)",
        "X-Amz-Cf-Id": "39uT89_umng7zPCNHqn3WdLlNJ6zFbQ5fmLVzJJNFcud-7pdZYJ1mg==",
        "X-AMZ-Date": "20171014T100929Z",
        "x-amz-security-token": "---",
        "X-Amzn-Trace-Id": "---",
        "X-Forwarded-For": "---",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https"
    },
    "queryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "path": "/production/test",
        "accountId": "037641059503",
        "resourceId": "hfza9c",
        "stage": "production",
        "requestId": "---",
        "identity": {
          ---
        },
        "resourcePath": "/test",
        "httpMethod": "POST",
        "apiId": "---"
    },
    "body": "{}",
    "isBase64Encoded": false
}

为什么身体总是空的?我是否必须初始化身体而不是我?还是有另一个身体被清除的阶段?

修改 我仍在努力,但调用正常的 AWS iOS SDK

let apiClient = APITestClient.default()
let apiGatewayRequest = AWSAPIGatewayRequest(httpMethod: "POST", urlString: "/test", queryParameters: nil, headerParameters: nil, httpBody: ["name":"example"])
apiClient.invoke(apiGatewayRequest).continueWith { task -> Void in ... }

完美无缺。为什么AWS甚至会为生成特殊的API网关SDK而烦恼?!

编辑2:API模型

public class APITest : AWSModel {

    var example: String?

    public override static func jsonKeyPathsByPropertyKey() -> [AnyHashable : Any]!{
        var params:[AnyHashable : Any] = [:]
        params["name"] = "name"

        return params
    }
}

然后由

调用
   public func testPost() -> AWSTask<APITest> {
        let headerParameters = [
                   "Content-Type": "application/json",
                   "Accept": "application/json",

                ]

        let queryParameters:[String:Any] = [:]

        let pathParameters:[String:Any] = [:]

        return self.invokeHTTPRequest("POST", urlString: "/test", pathParameters: pathParameters, queryParameters: queryParameters, headerParameters: headerParameters, body: nil, responseClass: APITest.self) as! AWSTask<APITest>
    }

4 个答案:

答案 0 :(得分:3)

我有完全相同的问题。无法找到解决方案 - 我将向亚马逊提供支持请求,并告知您是否有解决方案。非常令人沮丧。

答案 1 :(得分:3)

我不得不手动将@objcMembers属性添加到生成的参数模型类中:

@objcMembers public class MyParamsModel : AWSModel

答案 2 :(得分:0)

根据文档,您在AWSAPIGatewayRequest中输入了拼写错误

  

AWSAPIGatewayRequest(httpMethod:“POST”,urlString:“/ test”,   queryParameters:nil,headerParameters:nil,HTTPBody:   [ “名称”: “例如”])

<强>参考:

http://docs.aws.amazon.com/AWSiOSSDK/latest/Classes/AWSAPIGatewayRequest.html#//api/name/initWithHTTPMethod:URLString:queryParameters:headerParameters:HTTPBody

希望它有所帮助。

答案 3 :(得分:0)

我的请求正文也显示为空,我需要包括:

Content-Type:application/json

在请求标头中