gpt4 book ai didi

swift - Alamofire JsonEncoding 和 UrlEncoding

转载 作者:行者123 更新时间:2023-11-28 08:07:38 25 4
gpt4 key购买 nike

我需要使用 Alamofire 发送补丁请求,我的问题是我需要在此请求中同时使用 URLEncoding 和 JSONEncoding。

let url = URL(string: "https://kinto.dev.mozaws.net/v1/buckets/\(self.bucketName!)/collections/\(self.collectionName!)/records")!

parameters?["data"] = kintoDictionary

Alamofire.request(url, method: .patch, parameters: self.parameters, encoding: JSONEncoding.default, headers: self.headers).responseJSON { (response) in
...
}

如何在此请求中添加带参数的 URLEncoding ??我应该使用 NSURLRequest 而不是 URL 吗?

最佳答案

swift 3.0

您的网址需要删除空格,因此请使用以下代码对您的网址进行编码。

你可以试试这个

let url = URL(string: "https://kinto.dev.mozaws.net/v1/buckets/\(self.bucketName!)/collections/\(self.collectionName!)/records")!
print(url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
parameters?["data"] = kintoDictionary

Alamofire.request(url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!, method: .patch, parameters: self.parameters, encoding: URLEncoding.default, headers: self.headers).responseJSON { (response) in
...
}

关于swift - Alamofire JsonEncoding 和 UrlEncoding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44649758/

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