posttype-conversionxqueryrestxq

RestXQ [XPTY0004] Cannot convert empty-sequence() to document-node()


I have a RestXQ method:

declare
    %rest:path("/doSomething")
    %rest:POST
    %rest:form-param("name","{$name}")
    function page:doSomething($name as document-node())
    {
        (: Code... :)
    };

I tried to send a POST request to this method via XForms. As response on the client I get [XPTY0004] Cannot convert empty-sequence() to document-node(): (). I tried to remove document-node() but then the parameter $name is just empty.

The request parameter looks like this:

<data xmlns=""><name>Test</name></data>

Any solutions?


Solution

  • The problem is that %rest:form-param("name","{$name}") is for key-value pairs, but your method indicates that you want $name as document-node(). Those two things together don't make sense.

    Instead of the form-param you likely want the body of the POST request, for that you would use the following:

    declare
      %rest:path("/doSomething")
      %rest:POST("{$body}")
    function page:doSomething($body as document-node())
    {
        (: Code... :)
    };
    

    See http://www.adamretter.org.uk/papers/restful-xquery_january-2012.xhtml#media-example