UrlSession Timed out error received after POST request

I tried to Send a POST request to a website by using urlsession task and i can never get a response back after. What i can get is a error message which shows:

error=Optional(Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSErrorFailingURLStringKey=https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests/, NSErrorFailingURLKey=https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests/, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2103, NSLocalizedDescription=The request timed out.})

2018-10-11 13:35:47.594310+0200 SwishFinalTestingIOS[5264:2203153] Task <B7405BAC-9E65-4DAA-A8C6-BA5D826386D4>.<1> finished with error - code: -999

2018-10-11 13:35:47.595138+0200 SwishFinalTestingIOS[5264:2203154] Task <B7405BAC-9E65-4DAA-A8C6-BA5D826386D4>.<1> HTTP load failed (error code: -999 [1:89])


I followed this guide in stackoverflow: https://stackoverflow.com/questions/44564422/request-timed-out-with-code

but it dosen't help me to solve my problem.

i tried to extend the timeout interval to 120 but i still can't load the data.

When i run my code, i will stuck at session.dataTask(with: request) and then my request time running out.


my Info.plist:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>mss.cpc.getswish.net</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>
</dict>


my post request code:

let url = URL(string: "https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests/")!
        let params = [
            "payeePaymentReference": "0123456789",
            "callbackUrl": "https://example.com/api/swishcb/paymentrequests",
            "payeeAlias": "1231181189",
            "amount": "100",
            "currency": "SEK",
            "message": "Kingston USB Flash Drive 8 GB"
        ]
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        var headers = request.allHTTPHeaderFields ?? [:]
        headers["Content-Type"] = "application/json"
        request.allHTTPHeaderFields = headers
        guard let httpBody = try? JSONSerialization.data(withJSONObject: params, options: []) else {
            return
        }
        request.httpBody = httpBody
        print("jsonData: ", String(data: request.httpBody!, encoding: .utf8) ?? "no body data")
        // Create and run a URLSession data task with our JSON encoded POST request
        let config = URLSessionConfiguration.default
        config.timeoutIntervalForResource = 120
        config.timeoutIntervalForRequest = 120
        let session = URLSession(configuration: config, delegate: self, delegateQueue: OperationQueue.current)
        let task = session.dataTask(with: request) { (data, response, error) in
            guard error == nil && data != nil else {
                print("error=\(String(describing: error))")
                return
            }
            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(String(describing: response))")
                return
            }
            let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
            let responseStringForJson = "[\(responseString)]"
            print("result = \(String(describing: responseStringForJson))")
        }
        task.resume()

This is the format for sending the POST request code:

https://imgur.com/a/dpIX087

Replies

Just wondering what's the current status for my request now?

Just wondering what's the current status for my request now?

DevForums isn’t the right place to talk about DTS official business. Please drop me a line via email (my email address is in my signature) and I’ll take a look.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"