-
-
Save cstrahan/5ba3b9872707b0be6d6f to your computer and use it in GitHub Desktop.
Example how to use wreq with ssl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE OverloadedStrings #-} | |
| module Network.HttpSSL ( | |
| postSSL | |
| , SSLOptions (..) | |
| ) where | |
| {- | |
| build-depends: | |
| wreq | |
| , HsOpenSSL | |
| , http-client-openssl | |
| , lens | |
| -} | |
| import Control.Lens | |
| import Data.ByteString.Lazy (ByteString) | |
| import Network.HTTP.Client.OpenSSL | |
| import Network.Wreq | |
| import OpenSSL (withOpenSSL) | |
| import OpenSSL.Session (SSLContext) | |
| import qualified OpenSSL.Session as SSL | |
| data SSLOptions = SSLOptions { | |
| optionsClientCert :: FilePath | |
| , optionsCaCert :: FilePath } | |
| setupSSLCtx :: SSLOptions -> IO SSLContext | |
| setupSSLCtx (SSLOptions clientCert caCert) = | |
| do ctx <- SSL.context | |
| SSL.contextSetPrivateKeyFile ctx clientCert | |
| SSL.contextSetCertificateFile ctx caCert | |
| return ctx | |
| postSSL :: SSLOptions -- ^ Options | |
| -> String -- ^ URL | |
| -> ByteString | |
| -> IO (Response ByteString) | |
| postSSL sopts url b = | |
| let mkOpts c = defaults & manager .~ Left (opensslManagerSettings c) | |
| call o = postWith (mkOpts o) url | |
| in withOpenSSL $ call (setupSSLCtx sopts) b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment