1

我需要使用 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 吗?

4

1 回答 1

0

斯威夫特 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 
...
}
于 2017-06-20T10:29:11.490 回答