Alamofire JsonEncoding和UrlEncoding

时间:2017-06-20 09:54:08

标签: swift alamofire kinto

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

1 个答案:

答案 0 :(得分:0)

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 
...
}