Skip to content
Advertisement

How to pass URL as a path parameter to a FastAPI route?

I have created a simple API using FastAPI, and I am trying to pass a URL to a FastAPI route as an arbitrary path parameter.

JavaScript

When I test it, it doesn’t work and throws an error. I am testing it this way:

JavaScript

Advertisement

Answer

Option 1

You can simply use the path convertor to capture arbitrary paths. As per Starlette documentation, path returns the rest of the path, including any additional / characters.

JavaScript

or

JavaScript

Test using the below:

JavaScript

Output:

JavaScript

Test using HTML <form>:

If you would like to test the above by passing the URL through an HTML <form>, instead of manually typing it after the base URL, please have a look at Option 3 of this answer, which demonstrates how to convert the form <input> element into a path parameter on <form> submission.

Option 2

As @luk2302 mentioned in the comments section, your client (i.e., either end user, javascript, etc) needs to encode the URL. The encoded URL, however, as provided by @luk2302 does not seem to work, leading to a "detail": "Not Found" error. As it turns out, you would need to encode it twice to work. That is:

JavaScript

On server side, you can decode the URL (twice) as follows:

JavaScript

Option 3

Since your endpoint seems to accept POST requests, you might consider having the client sending the image URL in the body of the request, instead of passing it as a path parameter. Please have a look at the answers here, here and here, as well as FastAPI’s documentation, on how to do that.


Note:

If you are testing this through typing the aforementioned URLs into the address bar of a browser, then keep using @app.get() routes, as when you type a URL in the address bar of your browser, it performs a GET request. If , however, you need this to work with POST requests, you will have to change the endpoint’s decorator to @app.post() (as shown in your question); otherwise, you would come accross {"detail":"Method Not Allowed"} error.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement