【问题标题】:How to pass parameter to a post API in swift如何在swift中将参数传递给post API
【发布时间】:2020-08-27 16:36:51
【问题描述】:

我有一个 API,其参数如图所示。

如何在 swift 代码中给出这些参数。 我已经尝试过这种方式的代码,但它似乎不起作用。

let header: HTTPHeaders = ["Content-Type":"application/json","x-token": self.usertoken!]
let parameters = [ "request_type" : "nanny_submit_exam",
                          "exam_id": self.examid!] as [String : Any]

        AF.upload(multipartFormData: { (multipartFormData) in

                          for (key, value) in parameters {
                             multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
                          }
            for i in 0..<(self.listData?.nanniBookingDetails.questionsData.count)!
                                             {
                                                multipartFormData.append(self.answers[i].data(using: String.Encoding.utf8)!, withName: "answers[i][\(1)]['answer']")
                                            }
              }, to: mainURL+URLS.custom.rawValue, usingThreshold: UInt64.init(), method: .post, headers: header).responseJSON { response in
  switch response.result {
                   case .success(let json):
                       print("Validation Successful",json)
                       if let res = json as? [String: Any]{
}
 case let .failure(error):
                                     // self.loadingview.isHidden = true
                                      print(error)
                                      self.showAlert(message: "No internet connection")
                                  }
                   
               }

这是抛出 alamofire 错误 Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) 这意味着传递的参数是错误的。 你能告诉我这些参数哪里错了吗?因为它在邮递员中工作。

【问题讨论】:

    标签: json swift parsing alamofire


    【解决方案1】:

    选择保存按钮下方的“代码”,可以选择swift。这将为您提供您需要作为参数传递的集合

    【讨论】:

    • 我从邮递员中给出的代码中得到了预期的响应。但它更多的是静态数据。就像你看到我需要更多的 for 循环取决于数据的数量。也可以有 answer[0][3]['answer'] 。
    猜你喜欢
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 2019-01-28
    • 1970-01-01
    相关资源
    最近更新 更多