Skip to content

Add support for parsing URL query parameters with no value #3053

Open
@calebhailey

Description

@calebhailey

Is your feature request related to a problem? Please describe.

I'm developing an HTTP API using Vapor where some endpoints may offer support for URL query parameters that have a key but no value (e.g. ?foo vs ?foo=bar). As far as I can tell, the URI Spec doesn't specify that query parameters must have a value, so my assumption was that this should be possible.

Describe the solution you'd like

It should be possible to decode query parameters and capture keys with no values:

var params: Dictionary<String, String?> = try req.query.decode(Dictionary<String, String?>.self)

Describe alternatives you've considered

This works as expected but it appears to skip query params with no values:

var params: Dictionary<String, String> = try req.query.decode(Dictionary<String, String>.self)

I've tried switching the Dictionary value type to an optional string and it still won't decode url query parameters without values

var params: Dictionary<String, String?> = try req.query.decode(Dictionary<String, String?>.self)

NOTE: I suspect a Struct attribute with an optional value would work, but I have not tried decoding to a struct because in my case I need support for dynamic query parameter keys, thus the Dictionary<String, String>.

Additional context

Vapor appears to parse url params as long as there is an =; so ?foo= works, but ?foo does not get parsed.

PS – I'm new to Vapor but I'm loving it so far! Keep up the great work! 💪

EDIT: added link to the URI Spec

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions