Loading TOC...

xdmp:get-request-field

xdmp:get-request-field(
   $name as xs:string,
   [$default as xs:string?]
) as item()*

Summary

Returns the value of a named request field. If the request field is a multipart/form-data type in a POST form, you can use xdmp:get-request-field for file upload applications (see the second example below).

Parameters
$name Request field name.
$default A default value to return if there is no request field.

Example

xdmp:get-request-field("index")
=> "10"

Example

Consider a form.xqy XQuery module with the following content:

  <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <form name="test" action="upload.xqy?uid={xdmp:random()}" method="post"
          enctype="multipart/form-data">
    <p><label>File to upload:
    <input type="file" class="name" name="upload" size="50"/></label></p>
    <p><input type="submit" value="Upload and Get Results"/></p>
    </form>
    </body>
  </html>

Then have an upload.xqy XQuery module as follows:

 let $filename := xdmp:get-request-field-filename("upload")
 let $disposition := fn:concat("attachment; filename=""",$filename,"""")
 let $x := xdmp:add-response-header("Content-Disposition", $disposition)
 let $x:= xdmp:set-response-content-type(
             xdmp:get-request-field-content-type("upload"))
 return
 xdmp:get-request-field("upload")

Execute the form.xqy file, select a file, and click the
"Upload and Get Results" button.  The file you uploaded
will open according to the mime type the browser.  If you
wanted to save it to the database, you could use
xdmp:document-insert to do so.

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.

Comments

The commenting feature on this page is enabled by a third party. Comments posted to this page are publicly visible.
  • Can some one please provide the same example with rewriter option ? I am trying to submit the file with rewriter option and getting the 405 error with method not allowed. Not sure how to write the form with the rewriter. However ,when am not using the rewriter it is working fine .
  • Yes, the signature should say item()* for the return type, we will fix that in the next release.  The example does work as written for me, however, as the example relies on your browser to know what to do with the binary file (via a mimetype).  As for the document node, there are cases where that will make a difference and cases where it will not.  For example, if you save it to a document in the database, the document node should work OK.
  • This function actually returns something like item()*, not an xs:string*. The example is particularly misleading because in the case of a binary file upload, you'll actually get a document node back and you need to de-reference it, for example, xdmp:get-request-field("upload")/binary(), in order to use the binary content.
    • Yes, the signature should say item()* for the return type, we will fix that in the next release.  The example does work as written for me, however, as the example relies on your browser to know what to do with the binary file (via a mimetype).  As for the document node, there are cases where that will make a difference and cases where it will not.  For example, if you save it to a document in the database, the document node should work OK.