For Vapor script that runs on Linux I need to make a request that corresponds to following cURL but as of now, I was unable to find the best solution for this.
curl -X GET \
https://endpoint \
--cert ./sandbox.pem --key ./sandbox.key
PEM is of SHA256
Can you please navigate me to the best way to handle this?
I first tried implementing URLSessionDelegate's urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge) async -> (URLSession.AuthChallengeDisposition, URLCredential?)
but I had no idea what way of parsing the key I should choose.
I was trying using HTTPClient
, but that failed and said I should use MultiThreadedEventLoopGroup
let tlsConfig = TLSConfiguration.makeServerConfiguration(certificateChain: [.file(pemPath)], privateKey: .file(keyPath))
let client = HTTPClient(eventLoopGroupProvider: .createNew, configuration: .init(tlsConfiguration: tlsConfig))
defer {
client.shutdown()
}
let url = URL(string: "https:/endpoint")!
let request = try HTTPClient.Request(url: url, method: .GET)
let response = try await client.execute(request: request).get()
I did use it as:
let client = HTTPClient(eventLoopGroupProvider: .shared(MultiThreadedEventLoopGroup(numberOfThreads: 2)), configuration: .init(tlsConfiguration: tlsConfig))
But then I got an error
NIOPosix.NIOConnectionError(host: "endpoint", port: 443, dnsAError: nil, dnsAAAAError: nil, connectionErrors: [NIOPosix.SingleConnectionFailure(target: [IPv4] endpoint/%IP%, error: NIOSSL.NIOSSLError.failedToLoadPrivateKey)])
Thank you for any help/point at the right direction.