For Nim v1.4.0, the proc `net.newContext` has changed the interface with keys 
and certificates related files. You have to download the certificates pem files 
for example here: <https://curl.haxx.se/docs/caextract.html> and you can do
    
    
    import httpclient, net
    
    let ctx = newContext(cafile ="cacert.pem")
    let client = newHttpClient(sslContext = ctx)
    echo client.getContent("https://nim-lang.org";)
    
    
    Run

In case you don't have control with the certificates, you can resort to 
`CVerifyNone` , change the context initiation above with `let ctx = 
newContext(verifyMode = CVerifyNone)` (this is discouraged but it'll be helpful 
when in development and you can't be bothered with).

For older Nim version, or any version with `newContext` hasn't supported 
selecting the cert, you can workaround like this:
    
    
    import net, httpclient, openssl
    
    proc setBundle(ctx: SSlCtx, cabundle: cstring) =
      discard ctx.SSL_CTX_load_verify_locations(cabundle, nil)
    
    var ctx = newContext()
    ctx.context.setBundle("cacert.pem")
    var sslClient = newHttpClient(sslContext = ctx)
    echo sslClient.getContent("https://nim-lang.org";)
    
    
    Run

The above should handle the value returned from `openssl.SSL_CTX_LOAD` but I 
didn't put it in the example. You can see the openssl manual for that. Note 
that this workaround is only for client, not for server.

Reply via email to