Search code examples
swiftalamofiremultipartform-datamultipart

How to send space as space and not %20 in Alamofire multipart data upload?


I am using Alamofire to send a multipart upload for a image file . This is the code I am using to add the parameters . The problem is , it sends space as %20 (A k as A%20K) . I want to send it as it is (A K). This is the code for adding params

URLEncoding.default.queryParameters(params).forEach { (key, value) in
if let data = value.data(using: .utf8) {
multipart.append(data, withName: key)
print(String(data: data, encoding: String.Encoding.utf8) as Any)
}

Solution

  • Well you're using URLEncoding to query the parameters and those will always be URL encoded as you might expect.

    Try removing percentage encoding from the string before converting it to data like

    if let data = value.removingPercentEncoding?.data(using: .utf8) {
       // do your stuff here
    }
    

    Assuming your params object is an array of [String: String] values, you could simply do the following

    params.forEach { (key, value) in
       if let data = value.data(using: .utf8) {
          multipart.append(data, withName: key)
          print(String(data: data, encoding: .utf8))
       }
    }