HTTP Post Requests in Swift for beginners.

So this is probably annoying, but I know virtually nothing about swift. I'm trying to code an app, and I'm learning pretty well on the UI side of things, mostly because swiftUI makes that excessively easy, but I'm struggling with writing a post request for my sign in page. I'll link the code and the error below.
Thanks in advance
Code Block     func checkDetails(username: String, password: String) {
        guard let url =  URL(string:"https://api.rangouts.com/signin")
        else{
            return        }
        let body: [String: String] = ["username": username, "password": password]
        let finalBody = try? JSONSerialization.data(withJSONObject: body)
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.httpBody = finalBody
        
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        URLSession.shared.dataTask(with: request){
            (data, response, error) in
            
            guard let data = data else{
                return
            }
            print(data)
            
        }.resume()
        
    }
}




2020-11-14 09:14:05.286417-0800 rangouts[1174:110527] [] nwprotocolgetquicimageblockinvoke dlopen libquic failed

2020-11-14 09:15:05.834608-0800 rangouts[1174:111838] Task <85C62508-6220-4CF1-AB81-6949FED65ADF>.<1> finished with error [-1001] Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x600003e95e90 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={kCFStreamErrorCodeKey=-2102, kCFStreamErrorDomainKey=4}}, NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <85C62508-6220-4CF1-AB81-6949FED65ADF>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(

"LocalDataTask <85C62508-6220-4CF1-AB81-6949FED65ADF>.<1>"

), NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey="insert api", NSErrorFailingURLKey="insert api", _kCFStreamErrorDomainKey=4}




Message from debugger: Terminated due to signal 9

To elaborate, I'm running this on a simulator, which I know makes a difference though not exactly how, specifically a 14.1 simulator which I'm aware has some issues.

When I use just the link to my api, without "/signin" I get a smaller error:

nwprotocolgetquicimageblockinvoke dlopen libquic failed

But I also do receive a file back, 143 bytes, which I'm assuming is the JSON.

Sorry if the post is formatted incorrectly.




First of all, you should better ignore this log output (unless some Apple's engineer confirms it's harmful and need fixing):
Code Block
nw_protocol_get_quic_image_block_invoke dlopen libquic failed

(Better put log outputs in a code context, to avoid some characters work as markdown.)


When I use just the link to my api, without "/signin"

But I also do receive a file back, 143 bytes

That is suggesting you may not be sending a request in the right format for the signin API.
Are you sure JSON is the right format for the API?

One bad thing with your code is that you are silently disposing the error info: using try?, not touching error in completion handler.

Please try and see what happens with this code and tell us what you get:
Code Block
guard let url = URL(string:"https://api.rangouts.com/signin")
else{
return
}
//### This is a little bit simplified. You may need to escape `username` and `password` when they can contain some special characters...
let body = "username=\(username)&password=\(password)"
let finalBody = body.data(using: .utf8)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = finalBody
URLSession.shared.dataTask(with: request){
(data, response, error) in
print(response as Any)
if let error = error {
print(error)
return
}
guard let data = data else{
return
}
print(data, String(data: data, encoding: .utf8) ?? "*unknown encoding*")
}.resume()


HTTP Post Requests in Swift for beginners.
 
 
Q