Dear Christian and BaseX community,
working with multipart requests and RestXQ lately, we have the feeling that there could be a way to intercept the structure of the request with annotations in order to perform a more fine grained access to the single parts.
At the moment the docs state the following example where, due to the consumes annotation reporting multipart/mixed, the data is returned as a sequence of items:
declare
%rest:path
(
"/multipart"
)
%rest:POST
(
"{$data}"
)
%rest:consumes
(
"multipart/mixed"
)
(: optional :)
function
page:multipart(
$data
as
item
()*) {
"Number of items: "
||
count
(
$data
)
};
Do you think that an approach where the content
negotiation is refined to single parts could be implementable?
What we have in mind is something like the following
snippet.
Besides the usual sequence based representation of the body, parts are extracted as variables (part named “partname1” with $part1 and type validated/coerced to the function signature and so on).
declare
%rest:path
(
"/multipart"
)
%rest:POST
(
"{$data}”
) (:
{$data} Could be removed at all or is out of standard? :)
%rest:consumes
(
"multipart/mixed"
)
(: optional :)
%input:part
(
"{$part1}",
"partname1")
(: partname could be inferred from Content-Disposition and C
ontent-Type for type coercion applied as usually with single-part requests :)
%input:part
(
"{$part2}",
"partname2"
)
function
p
:multipart(
$data
as
item
()*, $part1
as
xs:binary, $part2
as
node()) {
p
:do-something-with($part1)
};
Maybe even the serialization method could be declared...
declare
%rest:path
(
"/multipart"
)
%rest:POST
(
"{$data}"
)
%rest:consumes
(
"multipart/mixed"
)
(: optional :)
%input:part
(
"{$part1}",
"partname"
, "csv;separator=','")
(: Just as an example ...:)
function
p
:multipart(
$data
as
item
()*, $part1 as node()) {
p
:do-something-with($part1)
};
Finally a check on the part count could be enforced in order to disallow requests that do not match
declare
%rest:path("/multipart")
%rest:POST("{$data}")
%rest:consumes("multipart/mixed") (: optional :)
%input:part("{$part1}", "partname", "csv,separator=','")
%input:part-count(2)
function p:multipart($data as item()*, $part1 as node()) {
p
:do-something-with($part1)
};
We understand that RestXQ is a shared formalization effort in the XML community but maybe there could be some opportunity to experiment and propose an addition to the specification?
As usual thanks a lot for your attention.
Best regards