Reputation: 5
I have POST request:
init (url: String,arg: String) {
self.responseString = nil
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "POST"
let postString = arg
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) {
data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
self.responseString = nil
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { //
self.responseString = nil
} else {
self.responseString = String(data: data, encoding: .utf8)
}
}
task.resume()
}
When arg have a character \n, half arg text is lost. How fix this problem?
Upvotes: 0
Views: 2014
Reputation: 470
strUrl = strUrl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let url = URL.init(string: strUrl)
let requestURL = URLRequest(url: url!)
Upvotes: 3