1

I am getting error while making POST request with swift 4(iOS) I am getting following debug description:

2017-11-09 23:12:57.283421+0300 ios1[35428:5830006] 
Task <23DDE1DF-B58F-4A9E-9BB1-21571EE25661>.<1> HTTP load failed (error code: -1004 [1:61])

My code:

let dict = ["link": web.text, "addr": edit.text]

guard let uploadData = try? JSONEncoder().encode(dict) else { return }

let actInd = showActivityIndicatory(uiView: appView)

let url = URL(string: host)!

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

let task = URLSession.shared.uploadTask(with: request, from: uploadData) { data, response, error in

    if error != nil {
        // handle the transport error
        result = 2
        print(error.debugDescription)
        return
    }

    guard let response = response as? HTTPURLResponse, response.statusCode == 201 else {
        result = 1
        return
    }

    if response.mimeType == "text/plain" || response.mimeType == "text/plain", let data = data {
        result = 0
        print(data)
    }
}

task.resume()

P.S.: I could see that request has been done correctly on server.

Thank you a lot

Ashley Mills
  • 41,127
  • 14
  • 115
  • 144
  • Strange, seems to be a "kCFURLErrorCannotConnectToHost" error (https://stackoverflow.com/questions/6778167/undocumented-nsurlerrordomain-error-codes-1001-1003-and-1004-using-storeki) but you say that you could connect to it. – Larme Nov 09 '17 at 21:00
  • I too, have a similar issue. I switched my code from Swift 3.3 to Swift 4.1 and my "http" request has stopped working. It was working fine before switching to Swift 4.1. The same request works fine on postman though. – Deepak Badiger Apr 17 '18 at 19:35
  • It means that the server is OFF – charlslvn Aug 26 '20 at 13:45

0 Answers0