New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should we support querystring / x-www-form-urlencoded
messages?
#247
Comments
x-www-form-urlencoded
) messages?x-www-form-urlencoded
messages?
They sound very reasonable. I specifically like the aspect of optionally coercing values into common expected types (int/bools and such)
I'd say so. If users need more complex parsing, they could always implement it on top of
This to me depends on the direction you want to take this library to be going. If it's - as you said - to provide general parsing / validation utilities commonly needed for webservers then this would be a good namespace, since it follows the established schema ( This brings me to my question regarding As already expressed on discord, I would welcome The question however is, what do you think the scope of this would be? If query strings, and |
This scheme makes a lot of sense for url queries. I have been using a similar scheme with msgspec to serialize parsed database query tokens to "kwarg-like" collections I can evil eval(), obviously with similar caveats. I like your idea better. |
I’m for it for the simple fact that the validation of POST JSON data and query string data should (in my mind) be handled by the same system. This makes producing error messages on bad requests consistent, as well as the interface for passing input data from the request to database operation handlers. I am currently using Falcon in an experimental system and it has support for plucking items out of a query string one at a time and coercing them into Python objects (e.g.: request.get_param_as_datetime), but it doesn’t have the same kind of constraints that msgspec has (for instance, enforcing a value is time zone aware). I would appreciate the ability to define a msgspec Struct and decode a query string into that object, or throw errors in the same way as a JSON payload. |
URL querystrings/
x-www-form-urlencoded
forms are structured but untyped messages. The python standard library has a few tools for encoding/decoding these:This is annoying to work with manually because the output is always of type
dict[str, list[str]]
. This means that:A library like
Pydantic
may be used to ease some of the ergonomic issues here, but adds extra overhead.Since
msgspec
is already useful for parsing JSON payloads into typed & structured objects, we might support a newquerystring
encoding/decoding that makes use of msgspec's existing type system to handle the decoding and validation. A lot of the code needed to handle this parsing already exists in msgspec, it's mostly just plumbing needed to hook things together. For performance, I'd expect this to be ~as fast as our existing JSON encoder/decoder.Proposed interface:
Proposed encoding/decoding scheme:
rails
orsinatra
do (i.e. nofoo[][key]=bar
stuff).type
must be a top-level object-like (struct, dataclass, ...) type, mapping fields to value typesThe following value types are supported
int
,float
,str
, and str-like types (datetimes, ...) map to/from their str representations, quoting as neededbool
serializes to"true"
/"false"
. When deserializing,""
,"1"
and"0"
are also accepted (are there other common values?)None
serializes as""
. When decoding"null"
is also accepted.list
/tuple
/...) map to/from multiple values set for a field. So a fielda
with value("x", None, True, 3)
would be"a=x&a=&a=true&a=3"
Questions:
encode
/decode
? The stdlib also exposes a few options that I've never needed to change:max_num_fields
to limit the number of fields when decodingseparator
to change the character used for separating fields (defaults to&
).msgspec.querystring
the best namespace for this format, or is there a better name we could use?msgpspec
to handle much of the parsing/validation that a typical web server would need to handle in a performant and useful way.The text was updated successfully, but these errors were encountered: