Hi Ash,

The easiest way to accept an XML document, no matter which encoding it has,
looks as follows:

(: your RESTXQ endpoint :)
declare
  %rest:POST('{$xml}')
  %rest:path('/test')
function local:xml($xml) {
  $xml//text()
};

# a command-line call to send the request
curl -H"Content-Type:application/xml" -XPOST -Ttest.xml "
http://localhost:8080/test";

Here is a slightly more complex example to upload the document via an HTML
form [1]:

declare
  %rest:GET
  %rest:path('/test')
  %output:method('html')
function local:f() {
  <form action="/test" method="POST" enctype="multipart/form-data">
    <input type="file" name="files" multiple="multiple"/>
    <input type="submit"/>
  </form>
};

declare
  %rest:POST
  %rest:path("/test")
  %rest:form-param("files", "{$files}")
function local:upload($files) {
  <results>{
    for $name    in map:keys($files)
    let $content := $files($name)
    return <result name='{ $name }'>{
      try {
        fetch:binary-doc($content)
      } catch * {
        attribute error { $err:description }
      }
    }</result>
  }</results>
};

The uploaded binary is converted to an XML document with fetch:binary-doc
[2]. An error is output if the input cannot be parsed as XML. Again, the
encoding doesn’t matter: It will dynamically be derived from either the
byte order mask, the first bytes or the embedded XML declaration.

It seems that’s the solution that would also help you to get the unit test
running, right? Maybe the standard function will be enhanced to also accept
xs:base64Binary in XQuery 4, but that’s still discussed [3].

Hope this helps,
Christian

[1] https://docs.basex.org/wiki/RESTXQ#File_Uploads
[2] https://docs.basex.org/wiki/Fetch_Module
[3] https://github.com/qt4cg/qtspecs/issues/748

Reply via email to