ios - API 网关 : Empty request body?

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

我正在尝试使用 AWS 生成的 iOS (Swift) SDKPOST 正文 发送到 AWS Lambda 函数API GATEWAY,但传输的主体始终为空。

为此,我建立了一个模型:

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

,我将其包含在 $Resources ->/test -> POST -> Method Execution -> Method Request -> Request Body 面板中。

然后我部署了 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 函数收到的body 总是空的 {},我在 API Gateway 日志中检查过:

(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
}

为什么 body 总是空的?我是否必须以与我不同的方式初始化主体?或者还有另一个阶段, body 被清除?


编辑: 我仍在挣扎,但调用了正常的 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>
    }

最佳答案

我遇到了完全相同的问题。尚未找到解决方案 - 我会向亚马逊提出支持请求,并告知您是否有解决方案。非常令人沮丧。

关于ios - API 网关 : Empty request body?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46743474/

相关文章:

amazon-web-services - 弹性 beantalk 实例如何与 route 53 相关联

java - POST 请求无法使用 ReSTLet 处理 JSON 输入

java - Spring post 重定向到 access-denied while get 工作

ios - PayPal MPL 的内部服务器错误

ios - 无法在 bundle 中加载 Nib - 错误的 nibname

mysql - 您对 amazon-rds 的性能有何体验

hadoop - 为什么 Hive "create external table"在 S3 上将数据存储在子文件夹 "-ext-10000"下?

post - 带有 Go 后端的 Flutter/Dart 前端中的发布请求问题

ios - 每次调用 API 时键的数量都发生变化,如何解析 JSON 数组?

ios - 带有文本和图像的 UIBarButtonItem - iOS 8