Marklogic 8 rest extension multpart/form-data with file upload issues
<p>I wrote a small rest extension in ML8 which basically stores the file uploaded into the ml content database.. when I do this I get the following exception </p>

<pre><code>&lt;error-response xmlns="http://marklogic.com/xdmp/error"&gt;
&lt;status-code&gt;500&lt;/status-code&gt;
&lt;status&gt;Internal Server Error&lt;/status&gt;
&lt;message-code&gt;INTERNAL ERROR&lt;/message-code&gt;
&lt;message&gt;XDMP-UPDATEFUNCTIONFROMQUERY: xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")($context, $service-params, $input) -- Cannot apply an update function from a query . See the MarkLogic server error log for further detail.&lt;/message&gt;
&lt;/error-response&gt;
</code></pre>

<p>But I did set the declare option xdmp:update "true"; Following is the code..</p>

<pre><code>module namespace repoLoad = "http://marklogic.com/rest-api/resource/repoLoad";

declare namespace rapi = "http://marklogic.com/rest-api";

declare default function namespace  "http://www.w3.org/2005/xpath-functions";
declare option xdmp:mapping "false";
declare option xdmp:update "true";

(: Function responding to GET method - must use local name 'get':)
declare function repoLoad:get($context as map:map, $params  as map:map) as document-node()*
{
    repoLoad:notSuportedMsg($context)
};

(: Function responding to PUT method - must use local name 'put'. :)
declare function repoLoad:put($context as map:map, $params  as map:map, $input as document-node()*) as document-node()?
{
    repoLoad:notSuportedMsg($context)
};

(: Func responding to DELETE method - must use local name 'delete'. :)
declare function repoLoad:delete($context as map:map,$params  as map:map) as document-node()?
{
    repoLoad:notSuportedMsg($context)
};

(: Function responding to POST method - must use local name 'post'. :)
declare function repoLoad:post($context as map:map, $params  as map:map,$input as document-node()*) as document-node()*
{
    let $filename := xdmp:get-request-field-filename("upload")
    let $contentType := xdmp:get-request-field-content-type("upload")
    let $log := xdmp:log("File Name : " || $filename)
    let $log := xdmp:log("Content Type : " || $contentType)


    let $uri := "/documents/"||$filename
    let $_ := xdmp:document-insert($uri, xdmp:get-request-field("upload"),(xdmp:default-permissions()), ("raw"))

    let $output := json:object()
    let $_ := (
        map:put($output, "Name", $filename),
        map:put($output, "Mime-Type", $contentType)
    )
    return document {xdmp:to-json($output)}

};



declare private function repoLoad:notSuportedMsg($context as map:map) as document-node()*
{
    let $_ := map:put($context, "output-status", (501, "Not Supported HTTP method"))
    let $output := json:object()
    let $errorResponse := json:object()
    let $_ := (
        map:put($errorResponse, "statusCode", 501),
        map:put($errorResponse, "message", "Not Supported HTTP method")
    )
    let $dummpy := map:put($output, "errorResponse", $errorResponse )
    return document {xdmp:to-json($output)}
}; 
</code></pre>

<p>And Following is error trace in log files</p>

<pre><code>2016-02-15 17:29:32.085 Notice: content-repo: XDMP-UPDATEFUNCTIONFROMQUERY: xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")($context, $service-params, $input) -- Cannot apply an update function from a query
2016-02-15 17:29:32.085 Notice: content-repo: in /MarkLogic/rest-api/lib/extensions-util.xqy, at 877:20,
2016-02-15 17:29:32.085 Notice: content-repo: in extut:call-service("repoLoad", "POST", xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy"), map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="accept-types"&gt;&lt;map:value xsi:type="xs:string"&gt;te...&lt;/map:map&gt;), map:map(), document{binary{""}}) [1.0-ml]
2016-02-15 17:29:32.085 Notice: content-repo:   $extension-name = "repoLoad"
2016-02-15 17:29:32.085 Notice: content-repo:   $method = "POST"
2016-02-15 17:29:32.085 Notice: content-repo:   $service = xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")
2016-02-15 17:29:32.085 Notice: content-repo:   $context = map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="accept-types"&gt;&lt;map:value xsi:type="xs:string"&gt;te...&lt;/map:map&gt;)
2016-02-15 17:29:32.085 Notice: content-repo:   $service-params = map:map()
2016-02-15 17:29:32.085 Notice: content-repo:   $input = document{binary{""}}
2016-02-15 17:29:32.085 Notice: content-repo: in /MarkLogic/rest-api/lib/extensions-util.xqy, at 792:14,
2016-02-15 17:29:32.085 Notice: content-repo: in extut:invoke-service("repoLoad", "POST", "query", xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy"), map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="accept-types"&gt;&lt;map:value xsi:type="xs:string"&gt;te...&lt;/map:map&gt;), map:map(), document{binary{""}}, fn:false()) [1.0-ml]
2016-02-15 17:29:32.085 Notice: content-repo:   $extension-name = "repoLoad"
2016-02-15 17:29:32.085 Notice: content-repo:   $method = "POST"
2016-02-15 17:29:32.085 Notice: content-repo:   $default-txn-mode = "query"
2016-02-15 17:29:32.085 Notice: content-repo:   $service = xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")
2016-02-15 17:29:32.085 Notice: content-repo:   $context = map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="accept-types"&gt;&lt;map:value xsi:type="xs:string"&gt;te...&lt;/map:map&gt;)
2016-02-15 17:29:32.085 Notice: content-repo:   $service-params = map:map()
2016-02-15 17:29:32.085 Notice: content-repo:   $input = document{binary{""}}
2016-02-15 17:29:32.085 Notice: content-repo:   $in-txn = fn:false()
2016-02-15 17:29:32.085 Notice: content-repo:   $txn-curr = "query"
2016-02-15 17:29:32.085 Notice: content-repo:   $annotation = ()
2016-02-15 17:29:32.085 Notice: content-repo:   $txn-mode = "query"
2016-02-15 17:29:32.085 Notice: content-repo: in /MarkLogic/rest-api/models/resource-model-query.xqy, at 254:8,
2016-02-15 17:29:32.085 Notice: content-repo: in rsrcmodqry:resource-post("repoLoad", map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="accept-types"&gt;&lt;map:value xsi:type="xs:string"&gt;te...&lt;/map:map&gt;), map:map(), document{binary{""}}, fn:false(), local:rsrcmod-callback#6) [1.0-ml]
2016-02-15 17:29:32.085 Notice: content-repo:   $resource-name = "repoLoad"
2016-02-15 17:29:32.085 Notice: content-repo:   $context = map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="accept-types"&gt;&lt;map:value xsi:type="xs:string"&gt;te...&lt;/map:map&gt;)
2016-02-15 17:29:32.085 Notice: content-repo:   $resource-params = map:map()
2016-02-15 17:29:32.085 Notice: content-repo:   $input = document{binary{""}}
2016-02-15 17:29:32.085 Notice: content-repo:   $in-txn = fn:false()
2016-02-15 17:29:32.085 Notice: content-repo:   $responder = local:rsrcmod-callback#6
2016-02-15 17:29:32.085 Notice: content-repo:   $service = xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")
2016-02-15 17:29:32.085 Notice: content-repo: in /MarkLogic/rest-api/models/resource-model-query.xqy, at 219:4,
2016-02-15 17:29:32.085 Notice: content-repo: in rsrcmodqry:exec-post(map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="content-type"&gt;&lt;map:value xsi:type="xs:string"&gt;mu...&lt;/map:map&gt;), map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="upload"&gt;&lt;map:value/&gt;&lt;/map:entry&gt;&lt;/map:map&gt;...XDMP-CHILDNODEKIND: element nodes cannot have binary node children...), document{binary{""}}, local:rsrcmod-callback#6) [1.0-ml]
2016-02-15 17:29:32.085 Notice: content-repo:   $headers = map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="content-type"&gt;&lt;map:value xsi:type="xs:string"&gt;mu...&lt;/map:map&gt;)
2016-02-15 17:29:32.085 Notice: content-repo:   $endpoint-params = map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="upload"&gt;&lt;map:value/&gt;&lt;/map:entry&gt;&lt;/map:map&gt;...XDMP-CHILDNODEKIND: element nodes cannot have binary node children...)
2016-02-15 17:29:32.085 Notice: content-repo:   $input = document{binary{""}}
2016-02-15 17:29:32.085 Notice: content-repo:   $responder = local:rsrcmod-callback#6
2016-02-15 17:29:32.085 Notice: content-repo: in /MarkLogic/rest-api/endpoints/resource-service-query.xqy, at 65:8 [1.0-ml]
2016-02-15 17:29:32.085 Notice: content-repo:   $params = map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="upload"&gt;&lt;map:value/&gt;&lt;/map:entry&gt;&lt;/map:map&gt;...XDMP-CHILDNODEKIND: element nodes cannot have binary node children...)
2016-02-15 17:29:32.085 Notice: content-repo:   $headers = map:map(&lt;map:map xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:map="http://marklogic.com/xdmp/map"&gt;&lt;map:entry key="content-type"&gt;&lt;map:value xsi:type="xs:string"&gt;mu...&lt;/map:map&gt;)
2016-02-15 17:29:32.085 Notice: content-repo:   $method = "POST"
2016-02-15 17:29:32.085 Notice: content-repo:   $body = document{binary{""}}
2016-02-15 17:29:32.216 Info: content-repo: Status 500: XDMP-UPDATEFUNCTIONFROMQUERY: xdmp:function(fn:QName("http://marklogic.com/rest-api/resource/repoLoad","post"), "/marklogic.rest.resource/repoLoad/assets/resource.xqy")($context, $service-params, $input) -- Cannot apply an update function from a query
</code></pre>