0

Sending a POST request to our NGINX Server works good with URLRequest and URLSession.shared.dataTask. I can login to my app but when I try a GET request my server has no log that the request reached him. Finally I get the timeout error. Important, I am using https. I also tried to use http for GET Requests. on my NGINX Server I set TLS to 1.2 only (I am not a system specialist, I did that in the nginx cfg file.

Error Domain=NSURLErrorDomain Code=-1001 "Zeitüberschreitung bei der Anforderung." UserInfo={NSUnderlyingError=0x60400005abe0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://myurl, NSErrorFailingURLKey=https://myurl, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=Zeitüberschreitung bei der Anforderung.}

I am sure that my code for the URLRequest and URLSession is correct because against localhost and our development environment I do not have any of those problems.

Thats my code to create my URLRequest

private func buildUrlRequest(jsonObject:[String: Any], connectionType:ConnectionTypes, url:String) -> NSMutableURLRequest? {
    let jsonDataSerialized:Data
    do {
        jsonDataSerialized = try JSONSerialization.data(withJSONObject: jsonObject)
    } catch {
        return nil
    }
    var request:NSMutableURLRequest
    if nextData {
        request = URLRequest(url: URL(string: self.nextRequestPage)!) as! NSMutableURLRequest
    } else {
        let tString = self.mBaseUrl + url
        if let encoded = tString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
            var finalurl = URL(string: encoded)
        {
            if connectionType == .GET {
                var tString:String
                tString=finalurl.absoluteString
                tString = tString.replacingOccurrences(of: "https", with: "http")
                finalurl = URL(string:tString)!
            }
            request = URLRequest(url: finalurl) as! NSMutableURLRequest
        } else {
            return nil
        }
    }
    request.httpMethod = connectionType.rawValue // i am using enum here
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.setValue("no-cache)", forHTTPHeaderField: "Cache-Control")
    request.httpBody = jsonDataSerialized
    if mUser != nil && mUser.getSessionId() != nil {
        request.addValue("Token " + mUser.getSessionId()!, forHTTPHeaderField: "Authorization")
    }
    request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
    request.timeoutInterval = 30.0
    return request
}

This is how I create the task... after the method body I am using task.resume()

let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in .... (and so on)

I spend more than hours to solve this problem... But I have no idea. I am not sure if the Problem is the Server Config or the Swift code...

Neneil
  • 105
  • 12
  • 1
    btw, you don't need `NSMutableURLRequest`, you can just keep the value as `URLRequest` in Swift. Also, you can build your URL more easily using `URLComponents`. – Sulthan Sep 28 '18 at 16:03
  • Thanks yes I know but while trying stuff around I also tried NSMutable... ;) – Neneil Sep 28 '18 at 16:30

1 Answers1

2

The major problem was jsonObject:[String: Any] ! I changed it to jsonObject:[String: Any]?

In my code I've created the body for each request no matter if it was a POST GET PUT or what ever. Without the http-body in a GET request I have no problem anymore, no timeout! I correctly receive the data as expected.

I've also found out that it is not necessary to set the nginx's or lets encrypt sll_protocols to TLS 1.2 only in my case.

I hope if someone else runs into this issue, they will find this post :)

lewis
  • 2,936
  • 2
  • 37
  • 72
Neneil
  • 105
  • 12