带有标题参数,查询参数和请求正文的Swift 3 Alamofire POST

时间:2017-05-16 18:20:33

标签: swift alamofire quickbooks intuit-partner-platform

我对Alamofire本身很新,所以请原谅我,如果这是一个简单的。  我想使用Alamofire发布一个银行账户对象直接付款,但是我一直收到以下错误:

  

错误域= NSCocoaErrorDomain代码= 3840"没有值。" UserInfo = {NSDebugDescription =无值。}

documentation注意到以下内容:

REQUEST URL
Sandbox Base URL: https://sandbox.api.intuit.com
Production Base URL: https://api.intuit.com

Operation: POST /quickbooks/v4/customers/<id>/bank-accounts
Content type: application/json

标头参数: Request-Id 必需

查询参数: id 必需

申请正文

{
   "name": "My Checking",
   "routingNumber": "XXXXX0358",
   "accountNumber": "XXXX4534",
   "accountType": "PERSONAL_CHECKING",
   "phone": "6047296480"
}

我的尝试如下:

func saveBankAccountToIntuitPayments() {
        let url = "https://api.intuit.com/quickbooks/v4/customers/" + userID! + "/bank-accounts"

        let parameters: Parameters = [
            "name": "John Doe",
            "routingNumber": "123321456",
            "accountNumber": "432123789012",
            "accountType": "PERSONAL_CHECKING",
            "phone": "55568925029"
        ]

ATTEMPT 1

        Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON(completionHandler: {
            response in
            print("running self.parseData(JSONData: response.data!)")
            self.parseData(JSONData: response.data!)
        })

ATTEMPT 2

      let requestId = userID! + getTimeNow()
      let header = [ "Request-Id" : requestId ]

      Alamofire.request(url: url, method: .post, headers: header, parameters: parameters, encoding: JSONEncoding.default).responseJSON(completionHandler: {
            response in
            print("running self.parseData(JSONData: response.data!)")
            self.parseData(JSONData: response.data!)
      })

ATTEMPT 3

        request(url, method: .post, parameters: parameters, headers: header).responseJSON(completionHandler: {
            response in

            print("running self.parseData(JSONData: response.data!)")
            self.parseData(JSONData: response.data!)
        })

    }

这是我的json解析器的代码:

 func parseData(JSONData: Data) {
        do {
            var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
            print("printing readable JSON (below)")
            print(readableJSON ?? "no JSON data was returned")
        } catch {
            print("errror occured (below)")
            print(error)
        }
    }

0 个答案:

没有答案