# Basic setup of HTTP server with MbedTLS

**URL:** <https://discourse.julialang.org/t/basic-setup-of-http-server-with-mbedtls/46233>\
**Category:** New to Julia\
**Tags:** https\
**Created:** [September 7, 2020, 11:32pm UTC](https://discourse.julialang.org/t/basic-setup-of-http-server-with-mbedtls/46233 "2020-09-07T23:32:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmiller/32/20805_2.png) [@mmiller](https://discourse.julialang.org/u/mmiller)\
**Post date:** [September 7, 2020, 11:32pm UTC](https://discourse.julialang.org/t/basic-setup-of-http-server-with-mbedtls/46233/1 "2020-09-07T23:32:18Z")

</div>

Could anyone please point me to an example where HTTP.jl and MbedTLS.jl are used to create a server with an https connection? Currently I’m trying to merge the docs of the two packages like this:

```julia
function live(req)
    return 200, "OK"
end

HTTP.@register(ROUTER, "/live", live)

function requestHandler(req)
    status, obj = HTTP.handle(ROUTER, req)
    return HTTP.Response(status, obj)
end

function run()
    entropy = MbedTLS.Entropy()
    rng = MbedTLS.CtrDrbg()
    MbedTLS.seed!(rng, entropy)
    ctx = MbedTLS.SSLContext()
    conf = MbedTLS.SSLConfig()
    MbedTLS.config_defaults!(conf)
    MbedTLS.authmode!(conf, MbedTLS.MBEDTLS_SSL_VERIFY_REQUIRED)
    MbedTLS.rng!(conf, rng)
    HTTP.serve(requestHandler, "0.0.0.0", 8082, sslconfig=MbedTLS.SSLConfig(false))
end

```

But when I try to access the /live endpoint I get:

```julia
ERROR: MbedTLS error code -31104: SSL - Processing of the ServerHello handshake message failed (edited) 

```

I’m pretty new to TLS/SSL stuff so could well be missing something basic here. Thanks

---

<div class="post-metadata">

**Author:** ![mmiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmiller/32/20805_2.png) [@mmiller](https://discourse.julialang.org/u/mmiller)\
**Post date:** [September 9, 2020, 6:29am UTC](https://discourse.julialang.org/t/basic-setup-of-http-server-with-mbedtls/46233/2 "2020-09-09T06:29:19Z")

</div>

Bit more digging and the following steps work.

## Install mkcert and generate certificate

[https://github.com/FiloSottile/mkcert](https://github.com/FiloSottile/mkcert)

## Configure server

replace run() in the post above with

```julia-auto
function run()
    tlsconfig = MbedTLS.SSLConfig("cert.pem", "cert_key.pem")
    HTTP.serve(requestHandler, "0.0.0.0", parse(Int, APP_PORT), sslconfig=tlsconfig)
end

```

The https endpoint can then be accessed.

---

<div class="post-metadata">

**Author:** ![ColinCaine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colincaine/32/7211_2.png) [@ColinCaine](https://discourse.julialang.org/u/ColinCaine)\
**Post date:** [December 20, 2020, 4:52pm UTC](https://discourse.julialang.org/t/basic-setup-of-http-server-with-mbedtls/46233/3 "2020-12-20T16:52:59Z")

</div>

Did this actually end up working for you? I get exceptions from MbedTLS.jl when browsers see the self-signed certificate and in general MbedTLS seems very happy to just blow up and crash the whole server.

---

<div class="post-metadata">

**Author:** ![mmiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmiller/32/20805_2.png) [@mmiller](https://discourse.julialang.org/u/mmiller)\
**Post date:** [December 27, 2020, 6:03am UTC](https://discourse.julialang.org/t/basic-setup-of-http-server-with-mbedtls/46233/4 "2020-12-27T06:03:28Z")

</div>

Yes I got it working in the end. If you can post your error and code maybe I or someone else here can help?

---

<div class="post-metadata">

**Author:** ![ColinCaine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colincaine/32/7211_2.png) [@ColinCaine](https://discourse.julialang.org/u/ColinCaine)\
**Post date:** [December 27, 2020, 3:07pm UTC](https://discourse.julialang.org/t/basic-setup-of-http-server-with-mbedtls/46233/5 "2020-12-27T15:07:07Z")

</div>

Thanks for the offer, but I can’t reproduce the errors now 😕

Here’s a complete working example, including certificate generation, that works for me, hopefully it will help others.

```nohighlight
using HTTP
using MbedTLS

# Generate a new self-signed certificate (I can't be bothered working out how to do this with MbedTLS!)
# `yes XX` just fills all the answers that openssl asks for interactively.
run(pipeline(`yes XX`, `openssl req -x509 -nodes -newkey rsa:2048 -keyout selfsigned.key -out selfsigned.cert`))

tlsconfig = MbedTLS.SSLConfig("selfsigned.cert", "selfsigned.key")

HTTP.serve(sslconfig=tlsconfig) do req
    return HTTP.Response("Hello World!")
end

```

---

<div class="post-metadata">

**Author:** ![mmiller](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmiller/32/20805_2.png) [@mmiller](https://discourse.julialang.org/u/mmiller)\
**Post date:** [January 5, 2021, 2:09am UTC](https://discourse.julialang.org/t/basic-setup-of-http-server-with-mbedtls/46233/6 "2021-01-05T02:09:02Z")

</div>

Glad you got it working 😀
