Alamofire JsonEncoding and UrlEncoding

2.2k Views Asked by At

I need to send patch request using Alamofire, my problem is I need to use both URLEncoding and JSONEncoding in this request.

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

how to add URLEncoding with parameters in this request ?? Should I use NSURLRequest instead of URL ?

1

There are 1 best solutions below

0
On

Swift 3.0

your url need to remove space, so encode your url using below.

you can try this

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