Buenos dias estoy tratando de realizar una petición POST a un web service, el json que estoy utilizando es :
{ "usuario": 1066, "password": 123 }
Pero siempre obtengo como resultado el siguiente error :
dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})))
¿Cual es la causa de este error?
Gracias
internal func request(message:String,api:String) throws -> String {
guard let url = URL.init(string: self.url + api)else{
throw PostClientErrors.invalidURL
}
var request = URLRequest(url: url )
request.httpMethod = "POST"
guard let data = message.data(using: .ascii) else{
throw PostClientErrors.encodingDataError
}
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.uploadTask(with: request, from: data){
data, response, error in
self.error = nil
if let error = error{
self.error = PostClientErrors.urlSessionError(error)
self.end = true
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200{
self.end = true
self.error = PostClientErrors.httpError(httpStatus)
return
}
guard let string = String(data: data!, encoding: .utf8) else{
self.end = true
self.error = PostClientErrors.decodingDaraError
return
}
self.data = self.dataString.data(using: String.Encoding.utf8)
self.dataString = string
self.end = true
}
task.resume()
while !self.end{
print (task.state.rawValue)
}
return dataString
}