---
title: "💭 Header Parameters - FastAPI"
description: "!https://fastapi.tiangolo.com/tutorial/header-params/#declare-header-parameters"
date: 2023-07-28
published: true
tags:
  - python
  - fastapi
  - webdev
  - thought
template: link
---


<div class="embed-card embed-card-external">
  <a href="https://fastapi.tiangolo.com/tutorial/header-params/#declare-header-parameters" class="embed-card-link" target="_blank" rel="noopener noreferrer">
    <div class="embed-card-image">
      <img src="https://fastapi.tiangolo.com/assets/images/social/tutorial/header-params.png" alt="Header Parameters - FastAPI — FastAPI framework, high performance, easy to learn, fast to code, ready for production" loading="lazy">
    </div>
    <div class="embed-card-content">
      <div class="embed-card-title">Header Parameters - FastAPI</div>
      <div class="embed-card-description">FastAPI framework, high performance, easy to learn, fast to code, ready for production</div>
      <div class="embed-card-meta">fastapi.tiangolo.com</div>
    </div>
  </a>
</div>


Getting request headers in fastapi has a pretty nice stetup, it allows you to get headers values as function arguments, 

I was able to use headers to detect if a request was made from htmx or not.

> If the request was made from htmx, then we want a html format, otherwise I'm probably hitting the api programatically from something like `curl` or `python`

``` python
@post_router.post("/post/")
async def post_post(
    request: Request,
    post: PostCreate,
    current_user: Annotated[User, Depends(try_get_current_active_user)],
    session: Session = Depends(get_session),
    is_hx_request: Annotated[str | None, Header()] = None,
) -> PostRead:
    "create a post"
    print('hx_request', hx_request)
    db_post = Post.from_orm(post)
    session.add(db_post)
    session.commit()
    session.refresh(db_post)
    if is_hx_request:
        return templates.TemplateResponse("post_item.html", {"request": request, "config": config, "post": db_post})
    return db_post

```

!!! 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>
