swift:如何在aws-api-gateway的生成的iOS SDK中的PUT请求中设置主体?

时间:2019-02-22 10:53:46

标签: ios swift amazon-web-services http aws-api-gateway

这是PUT请求的代码,我想将TokenId作为JSON发送到api:我该怎么做?

 public func paymentPut(tokenId: String) -> AWSTask<AnyObject> {
    let headerParameters = [
        "Content-Type": "application/json",
        "Accept": "application/json",

        ]

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

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

    return self.invokeHTTPRequest("PUT", urlString: "***", pathParameters: pathParameters, queryParameters: queryParameters, headerParameters: headerParameters, body: nil, responseClass: nil)
}

我尝试将主体设置为此,但是我的应用程序崩溃了:

    let body: [String:Any] = ["token": tokenId]

崩溃后,错误是:     ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'+ [_ TtGCs26_SwiftDeferredNSDictionarySSP__ JSONKeyPathsByPropertyKey]:无法识别的选择器已发送给类

1 个答案:

答案 0 :(得分:0)

好的,所以这将来可能会对某些人有所帮助。这是我解决的方法: 1)创建一个AWS数据模型

import Foundation   
import AWSCore
@objcMembers
public class TESTPaymentPUT : AWSModel {

var token: String?

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

    return params
}
}

2)在视图控制器中设置以下值:

let tokenBody = TESTPaymentPUT()
tokenBody?.token = token

3)调用API方法,并将tokenBody设置为视图控制器中的参数:

API.client.paymentPut(body: tokenBody).continueOnSuccessWith { (_) -> Any? in
                return nil
            }

4)更改API方法:

public func paymentPut(body: TESTPaymentPUT?) -> AWSTask<AnyObject> {
    let headerParameters = [
        "Content-Type": "application/json",
        "Accept": "application/json",

        ]

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

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

    return self.invokeHTTPRequest("PUT", urlString: "***", pathParameters: pathParameters, queryParameters: queryParameters, headerParameters: headerParameters, body: body, responseClass: nil)
}