Enable https using UVICORN

I tried this tutorial (https://dev.to/rajshirolkar/fastapi-over-https-for-development-on-windows-2p7d) and then when I went to my browser but I cannot access my localhost address using https, if I try http it works but I need to use HTTPS.

Here is the code I tried:

import uvicorn

if __name__ == "__main__":
    uvicorn.run("app.api:app",
                 host="localhost",
                 port=8432, 
                 reload=True,
                 ssl_keyfile="./key.pem", 
                 ssl_certfile="./cert.pem")

Upvotes: 6

Views: 34396

Answers (2)

Juan Emilio
Juan Emilio

Reputation: 181

Try with this

if __name__ = 'main':
    uvicorn.run(
               app,
               host="0.0.0.0",
               port=8432,
               ssl_keyfile="./localhost+4-key.pem",
               ssl_certfile="./localhost+4.pem"
               )

If you pass directly your app instead the description of attributes app.api:app you can't use the automatic reload and the workers, but it is an easy way to test if the things are good configured.

Recommendations:

Ensure you are running this command: mkcert localhost 127.0.0.1 ::1 in the folder of your project, also you are probably writing wrong the name of the ssl certificates.

Upvotes: 8

btlam87
btlam87

Reputation: 83

If you got this error: "Unsupported upgrade request". Try install uvicorn[standard].

Upvotes: 0

Related Questions