0

Hello Stackoverflow Community,

I have a FastAPI based Python get request, where I wish to capture image URL as parameter. The following is the code snippet:

@app.get("/v1/image/{image_name}")
async def image_resize(image_name:str):
    image_file_name = image_name.split("/")[-1]

Intended URL is like http://localhost:8000/v1/image/https://www.freeimages.com/photo/circulate-abstract-1562332.jpg

where the image source URL is https://www.freeimages.com/photo/circulate-abstract-1562332.jpg

however, I am getting 404

INFO:     127.0.0.1:58611 - "GET /v1/image/https%3A//www.freeimages.com/photo/circulate-abstract-1562332.jpg HTTP/1.1" 404 Not Found

What would be the correct way to accept URL as request parameter. This service would be called by another API for sending the image source. Please feel free to suggest any better option to accept the URL.

Thank you in anticipation

JarroVGIT
  • 4,291
  • 1
  • 17
  • 29
Jony Hu
  • 13
  • 2

1 Answers1

0

You can define a path as type of the path parameter like so:

@app.get("/v1/image/{image_name:path}")
async def image_resize(image_name:str):
    image_file_name = image_name.split("/")[-1]
JarroVGIT
  • 4,291
  • 1
  • 17
  • 29
  • Thanks - that helped. However, image_name is now getting encodeURIComponent(https://www.freeimages.com/photo/circulate-abstract-1562332.jpg) - how do I get the URL from this? – Jony Hu Jul 28 '22 at 05:37
  • That link isn’t working – JarroVGIT Jul 28 '22 at 08:03
  • may be the image has been pulled down by the host. But the problem is that when I use @app.get("/v1/image/{image_name:path}"), and invoke the API from browser like http://localhost:8000/v1/image/https://www.freeimages.com/photo/circulate-abstract-1562332.jpg, the value captured in image_name is encodeURIComponent(freeimages.com/photo/circulate-abstract-1562332.jpg). How can I get only the URL – Jony Hu Jul 28 '22 at 08:24