---
title: "💭 fastapi https url_for"
description: "!None"
date: 2024-03-24
published: true
tags:
  - fastapi
  - webdev
  - thought
template: link
---


<!-- embed not found: None -->
![[None]]


jinja's `url_for` in fastapi does not account for https by default, there is
probably a better way, but this is a way that allows me to configure when I use
http vs https.

``` python
@pass_context
def https_url_for(context: dict, name: str, **path_params: Any) -> str:
    """
    always convert http to https
    """
    request = context["request"]
    http_url = request.url_for(name, **path_params)
    return str(http_url).replace("http", "https", 1)


def get_templates(config: BaseSettings) -> Jinja2Templates:
    templates = Jinja2Templates(directory="templates")
    templates.env.globals["https_url_for"] = https_url_for

    ## only use the default url_for for local development, for dev, qa, and prod use https
    if os.environ.get("ENV") in ["dev", "qa", "prod"]:
        templates.env.globals["url_for"] = https_url_for
        console.print("Using HTTPS")
    else:
        console.print("Using HTTP")

    return templates
```


!!! note

    This post is a <a href="/thoughts/" class="wikilink" data-title="Thoughts" data-description="These are generally my thoughts on a web page or some sort of url, except a rare few don&#39;t have a link. These are dual published off of my..." data-date="2024-04-01">thought</a>. It's a short note that I make
    about someone else's content online <a href="/tags/thoughts/" class="hashtag-tag" data-tag="thoughts" data-count=2 data-reading-time=3 data-reading-time-text="3 minutes">#thoughts</a>
