I'm building an app that makes a http POST call to my own backend, hosted on google cloud platform. The app crashes randomly, due to NSURLErrorDomain Code=-1017
Task <718E46A2-8F9E-45C2-A324-38320C24E4D1>.<4> HTTP load failed, 1063/0 bytes (error code: -1017 [4:-1])
Task <718E46A2-8F9E-45C2-A324-38320C24E4D1>.<4> finished with error [-1017] Error Domain=NSURLErrorDomain Code=-1017 "nie można zanalizować odpowiedzi" UserInfo={_kCFStreamErrorCodeKey=-1, NSUnderlyingError=0x2823a2d60 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorCodeKey=-1, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <718E46A2-8F9E-45C2-A324-38320C24E4D1>.<4>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <718E46A2-8F9E-45C2-A324-38320C24E4D1>.<4>"
), NSLocalizedDescription=nie można zanalizować odpowiedzi, NSErrorFailingURLStringKey=https://api.dev.<backend>.co/payment/bm/pay, NSErrorFailingURLKey=https://api.dev.<backend>.co/payment/bm/pay, _kCFStreamErrorDomainKey=4}
Runner/NewsViewController.swift:213: Fatal error: unexpected response
Runner/NewsViewController.swift:213: Fatal error: unexpected response
* thread #47, queue = 'com.apple.NSURLSession-delegate', stop reason = Fatal error: unexpected response
frame #0: 0x00000001acaca458 libswiftCore.dylib`_swift_runtime_on_report
libswiftCore.dylib`:
-> 0x1acaca458 <+0>: ret
libswiftCore.dylib`:
0x1acaca45c <+0>: b 0x1acaca458 ; _swift_runtime_on_report
libswiftCore.dylib`:
0x1acaca460 <+0>: adrp x8, 334844
0x1acaca464 <+4>: ldrb w0, [x8, #0x65c]
Target 0: (Runner) stopped.
This happens absolutely randomly. I've seen a couple of issues here on the apple forum, but none of them clearly describes what is the reason behind this error.
The request is simply built as follows:
request.httpMethod = "POST" //set http method as POST
request.allowsCellularAccess = true;
request.allowsExpensiveNetworkAccess = true;
request.allowsConstrainedNetworkAccess = true;
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) // pass dictionary to nsdata
} catch let error {
Amplitude.instance().logEvent("Error serializing POST parameters", withEventProperties: parameters)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue(appDelegate.variables!["X-UserPermission"] as! String, forHTTPHeaderField: "X-UserPermission")
request.addValue(appDelegate.variables!["Authorization"] as! String, forHTTPHeaderField: "Authorization")
request.httpShouldUsePipelining = true;
Selecting any option will automatically load the page