I don't have much experience with URLSession, but I'm trying to do a POST call, and I keep getting two different errors.
Here is my function:
func test() {
let parameters = ["username": "myUsername", "password": "myPassword"]
guard let url = URL(string: "https://example.com:443/Auth/") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print(error)
}
}
}.resume()
}
And here are the errors that I'm running into:
This first one has a stack overflow link: CredStore Perform Query error. I added the code that they said to add, but I ended up having the same errors.
CredStore - performQuery - Error copying matching creds. Error=-25300, query = {
class = inet;
"m_Limit" = "m_LimitAll";
ptcl = htps;
"r_Attributes" = 1;
sdmn = "/Auth/";
srvr = "example.com";
sync = syna;
}
The next error that I get is:
<NSHTTPURLResponse: 0x600000b94180> { URL: https://example.com:443/Auth/ } { Status Code: 401, Headers {
"Content-Length" = (
31
);
"Content-Type" = (
"text/plain"
);
Date = (
"Mon, 24 Sep 2018 23:16:45 GMT"
);
"Www-Authenticate" = (
"Basic realm=\"/Auth/\""
);
} }
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
I'm getting a 401 Unauthorized error. The only reason I can see it giving me 401 is that it isn't reading in the username/password correctly? I'm not 100% sure if the header titles "username" and "password" are correct. The actual username and password are definitely correct.