Skip to content

[Feature Request] Automatically URL-decode path parameters #164

@PhilippMDoerner

Description

@PhilippMDoerner

Heyho, as part of the webapp I build with prologue I also have endpoints that perform searches on my db via path parameters in the URL.

As such, I want the path parameter in the URL to be URL-decoded because I'm not going to have anything with %20 stored in my database. However, when using prologue normally it appears to not decode the URL path parameters, as observed with this minimal setup:

import prologue
import std/[strformat]

const SEARCH_TEXT_PARAM* = "searchText"
const SEARCH_TEXT_PATTERN* = fmt r"(?P<{SEARCH_TEXT_PARAM}>[^/]+)"

proc controller(ctx: Context) {.async.} =
    let searchText: string = ctx.getPathParamsOption(SEARCH_TEXT_PARAM).get()
    echo searchText

    await ctx.respond(Http200, searchText)

proc main() =
    var app: Prologue = newApp()
    app.addRoute(
        re fmt"/test/{SEARCH_TEXT_PATTERN}",
        handler = controller,
        httpMethod = HttpGet,
    )
    app.run()

main()

When calling this endpoint with the URL {{host}}/test/Walumba bla it will put Walumba%20bla in searchText, which is not what I would want. Luckily this is a pretty easy fix, as std/uri provides a decodeUrl proc that can be used like this to fix it:

ctx.getPathParamsOption(SEARCH_TEXT_PARAM).get().decodeUrl()

However, I think it makes sense to possibly have that be the default behaviour of prologue. Would this be alright? If so, I'd throw in a small PR in which I would add a call to decodeUrl in front of getPathParamsOption, getPathParams, getQueryParamsOption and getQueryParams.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions