Post

Replies

Boosts

Views

Activity

How to accept a self-signed SSL certificate with URLSession and Combine
I'm using URLSession and Combine to monitor and control a device on my internal network. One URL to retrieve JSON data is https://ip_address/api/v1/items The device has a self-signed certificate so I'm getting an error that the certificate is invalid. I need my code to ignore the certificate and retrieve the data. Is there a good solution to accept this self-signed certificate with Combine and URLSession? Here is the code that makes the HTTPS get request.     func sendControllerGetRequest(uri: String) -> PassthroughSubject<Data, Never> {         getRequestPublisher = PassthroughSubject<Data, Never>()         var urlRequest = URLRequest(url: URL(string: self.baseURI+uri)!)         urlRequest.httpMethod = "GET"         urlRequest.setValue("Bearer \(self.directorBearerToken)", forHTTPHeaderField: "Authorization")         getRequestCancellable = URLSession.shared.dataTaskPublisher(for: urlRequest)             .map{ $0.data }             .sink(receiveCompletion: { completion in                 switch completion {                 case .failure(let error):                     print("sendControllerGetRequest error")                     print(error)                 case .finished:                     print("sendControllerGetRequest finished")                     break                 }             }, receiveValue: { requestDetails in                print(String(data: requestDetails, encoding: .utf8)!)                 print("sendControllerGetRequest()")                 self.getRequestPublisher.send(requestDetails)                 self.getRequestPublisher.send(completion: .finished)             }             )         return getRequestPublisher     }
1
0
7.8k
Dec ’21