gpt4 book ai didi

ios - Swift 4 - 使用 httpbody 的 Alamofire 发布请求

转载 作者:行者123 更新时间:2023-11-28 07:19:35 29 4
gpt4 key购买 nike

我有这样的 Alamofire post 请求:

var jsonArrayOfDictionaries = [[AnyHashable: Any]]()

let user = appDelegate.username
let password = appDelegate.password

let url = webservice + "PostTasks"
let credential = URLCredential(user: user!, password: password!, persistence: .forSession)
let headers = ["Accept": "application/json;odata=verbose", "Content-type": "application/json;odata=verbose"]
let jsonData: Data? = try? JSONSerialization.data(withJSONObject: jsonArrayOfDictionaries, options: .prettyPrinted)

print(jsonData!)

//request.httpBody = jsonData

Alamofire.request(url, method: .post, headers: headers).authenticate(usingCredential: credential).responseJSON {
(response) in
switch response.result {
case .success:
if let value = response.result.value {

print(value)

OperationQueue.main.addOperation({

completion(true)

})


}else{

print("There is error in the server response")

completion(false)

}

case .failure (let error):

print("The NTLM request error is: ", error.localizedDescription)

completion(false)

}

}

我的问题是如何将 jsonData 变量添加到此请求的 httpbody 中?我调查了这个问题,所有的解决方案似乎都过时了。请帮忙!

这就是 jsonArrayOfDictionaries 的填充方式:

var jsonArrayOfDictionaries = [[AnyHashable: Any]]()

for i in 0..<cellHolder.count {

var jsonDict = [AnyHashable: Any]()

jsonDict["scheduleTaskID"] = cellHolder[i].scheduleTaskID

jsonDict["task"] = cellHolder[i].task

jsonDict["scheduledDate"] = cellHolder[i].scheduledDate

jsonDict["actualDate"] = cellHolder[i].actualDate

jsonDict["finishedDate"] = cellHolder[i].finishedDate

jsonDict["selected"] = (cellHolder[i].selected) ? 1 : 0

jsonDict["completedBy"] = appDelegate.username

jsonDict["sortOrder"] = cellHolder[i].sortOrder

jsonArrayOfDictionaries.append(jsonDict)

jsonDict = [AnyHashable: Any]()

}

它在一个循环中并被追加。

最佳答案

<强>1。改变

let jsonData: Data? = try? JSONSerialization.data(withJSONObject: jsonArrayOfDictionaries, options: .prettyPrinted)

让 jsonData = 试试 JSONSerialization.jsonObject(with: jsonArrayOfDictionaries, options: []) 作为? [字符串:任意]

<强>2。将 jsonData 添加到您的请求中

Alamofire.request(url, method: .post, headers: headers, parameters: jsonData).authenticate(usingCredential: credential).responseJSON {

关于ios - Swift 4 - 使用 httpbody 的 Alamofire 发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58542612/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com