---
title: "💭 URL Decoding query strings or form parameters in Python | URLD..."
description: "!https://www.urldecoder.io/python/"
date: 2023-07-28
published: true
tags:
  - python
  - urlib
  - fastapi
  - thought
template: link
---


<div class="embed-card embed-card-external">
  <a href="https://www.urldecoder.io/python/" class="embed-card-link" target="_blank" rel="noopener noreferrer">
    <div class="embed-card-content">
      <div class="embed-card-title">URL Decoding query strings or form parameters in Python | URLDecoder</div>
      <div class="embed-card-description">URL Decode online. URLDecoder is a simple and easy to use online tool for decoding URL components. Get started by typing or pasting a URL encoded string in the input text area, the tool will automa...</div>
      <div class="embed-card-meta">urldecoder.io</div>
    </div>
  </a>
</div>


In order to turn url encoded links back into links that I would find in the database of my thoughts project I need to urldecode them when they hit the api.  When anything hits the api it must urlencode the links in order for them to be sent correctly as data and not get parsed as part of the url.


> Here is a snippet of how I am using urlib.parse.unquote to un-encode encoded urls so that I can fetch posts from the database.

``` python
@post_router.get("/link/")
async def get_post_by_link(
    *,
    session: Session = Depends(get_session),
    link: str,
) -> PostRead:
    "get one post by link"
    link = urllib.parse.unquote(link)
    print(f'link: {link}')
    post = session.exec(select(Post).where(Post.link==link)).first()
    if not post:
        raise HTTPException(status_code=404, detail=f"Post not found for link: {link}")

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