I am having trouble calling APIs with URLSession
, but it works fine with AF.request
. I used URLSession
in all places where API calls were made, so I do not want to change with AF.
Code:
let parameters = [
"fk_EmpGLCode": "\(fk_EmpGLCode)",
"varClientName": "\(varClient)",
"strAppType": "IOS","strVersion": "\(VERSION_NUMBER)","varIMEINumber":
"\(ECP_Constant.varIMEInumber)","UserName": "\(userName)","Filename":
"\(zipName).zip","FileArray":
"\(fileData1)","varAppType":"\(Constan
.varAppType)"
]
var request = URLRequest(url: URL(string: web)!)
request.setValue(SyncConstant.HeaderToken, forHTTPHeaderField: "varToken")
request.httpMethod = "POST"
var jsonData1 = NSData()
do {
jsonData1 = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) as
NSData
} catch let error as NSError {
NSLog("delete directory \(error.debugDescription)")
}
request.httpBody = jsonData1 as Data
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = 30.0
sessionConfig.timeoutIntervalForResource = 60.0
let session = URLSession(configuration: sessionConfig)
let task = session.dataTask(with: request) { (data, response, error) in
if(data != nil) {
if let httpResponse = response as? HTTPURLResponse {
print("Status Code: \(httpResponse.statusCode)")
}
var jsonData = String(data: data!, encoding: String.Encoding.utf8) as String?
When I use URLSession to make an API call, I receive an error.
{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}
I tried with diffrent methods of API Call.