---
title: "Header Parameters - FastAPI"
description: "!https://fastapi.tiangolo.com/tutorial/header-params/#declare-header-parameters"
date: 2023-07-28
published: true
tags:
  - dev
  - fastapi
  - python
  - thought
  - webdev
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-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

```
