question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot pass URL Encoded string containing '/' as path parameter to GET endpoint

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

@app.get('/images/thumbnail/{filename}',
  response_description="Returns a thumbnail image",
  response_class="StreamingResponse",
  responses= {200: {"description": "an image", "content": {"image/jpeg": {}}}})
async def thumbnail_image (filename: str):

Description

I have an endpoint that needs to take a filename (or path to filename) parameter string that could be something like “myimage.jpg” (which works) or something like “path/to/myimage.jpg” (which doesn’t work)

http://localhost:8000/images/thumbnail/landscapes%2FIMG_1462.jpeg does not enter the endpoint above and gets back a 404

I’ve tried removing the {filename} path param to form a regular query parameter but it doesn’t help. I am using the Javascript encodeURIComponent to create the string that is being sent as the parameter.

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.70.1

Python Version

3.8

Additional Context

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

3reactions
ypsahcommented, Jan 23, 2022

Reading about it again, it looks like starlette supports a path convertor.

The following should do what you want:

from fastapi import FastAPI

app = FastAPI()

@app.get('/images/thumbnail/{filename:path})
async def thumbnail_image(filename: str) -> str:
    """
    A thumbnail image
    """
    return filename
1reaction
d13g0commented, Sep 7, 2022

Hi, my workaround to this issue was passing a base64 encoded variable in the querystring. My FastAPI endpoint looks like this:

from base64 import urlsafe_b64decode

@router.get('/my_route')
def my_endpoint(url:str = None):
    
    if url is None:
           raise HTTPException(...)
           
    parsed_url = urlsafe_b64decode(url)
    crud.operation(parsed_url,...)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass URL as a path parameter to a FastAPI route?
Option 1. You can simply use the path convertor to capture arbitrary paths. As per Starlette documentation, path returns the rest of the ......
Read more >
Pass API Gateway REST API parameters to a Lambda ...
I need my Amazon API Gateway REST API to pass query string parameters to a backend AWS Lambda function and an HTTP endpoint....
Read more >
Azure API Management transformation policies | Microsoft Learn
Reference for the transformation policies available for use in Azure API Management. Provides policy usage, settings, and examples.
Read more >
Describing Parameters - Swagger
A URL can have several path parameters, each denoted with curly braces { } . ... (strings, numbers, booleans) or arrays of primitives...
Read more >
any way to not use URL encoding? - OutSystems
Dynamic routes with special characters - any way to not use URL ... query string (also supported) we have to send these parameters...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found