Search code examples
pythonswaggerfastapiswagger-uiurlencode

How to pass unencoded URL in FastAPI/Swagger UI via GET method?


I would like to write a FastAPI endpoint, with a Swagger page (or something similar) that will accept a non-encoded URL as input. It should preferably use GET, not POST method.

Here's an example of a GET endpoint that does require double-URL encoding.

@app.get("/get_from_dub_encoded/{double_encoded_url}")
async def get_from_dub_encoded(double_encoded_url: str):
    """
    Try https%253A%252F%252Fworld.openfoodfacts.org%252Fapi%252Fv0%252Fproduct%252F7622300489434.json
    """
    original_url = urllib.parse.unquote(urllib.parse.unquote(double_encoded_url))
    response = requests.get(original_url)
    return response.json()

Which generates a Swagger interface as below.

The following PUT request does solve my problem, but the simplicity of a GET request with a form is better for my co-workers.

class InputModel(BaseModel):
    unencoded_url: AnyUrl = Field(description="An unencoded URL for an external resource", format="url")


@app.post("/unencoded-url")
def unencoded_url(inputs: InputModel):
    response = requests.get(inputs.unencoded_url)
    return response.json()

How can I deploy a convenient interface like that without requiring users to write the payload for a PUT request or to perform double URL encoding?

enter image description here

This post has some helpful related discussion, but doesn't explicitly address the FORM solution: How to pass URL as a path parameter to a FastAPI route?


Solution

  • You can use Form instead of query parameters as payload.

    from fastapi import FastAPI, Form
    import requests
    
    app = FastAPI()
    
    @app.post("/")
    def get_url(url: str = Form()):
    
        """
        Try https://world.openfoodfacts.org/api/v0/product/7622300489434.json
        """
        response = requests.get(url)
        return response.json()
    

    Swagger interface would look like: enter image description here

    You'll need to install python-multipart.

    Tip: Don't use async endpoint if you are using requests or any non-async library.