Loading TOC...

MarkLogic 9 Product Documentation
xdmp:http-post

xdmp:http-post(
   $uri as xs:string,
   [$options as (element()|map:map)?],
   [$data as node()?]
) as item()+

Summary

Sends an http POST request to the server.

Parameters
uri The URI to which the data is to be posted.
options Options with which to customize this operation. You can specify options as either an XML element in the "xdmp:http" namespace, or as a map:map. The options names below are XML element localnames. When using a map, replace the hyphens with camel casing. For example, "an-option" becomes "anOption" when used as a map:map key. This function supports the following options, plus the options from the xdmp:http-get function.

<data>

This option can contain any string. Anything in the data option is sent as a string in the PUT or POST body. When POSTing to a web service, the data may need to be a SOAP XML structure, a JSON structure, or binary content. The optional third argument can be used to specify the body instead.

The other options are the same as the other xdmp:http-* functions, and the options are documented with the xdmp:http-get $options parameter.

data The data for this request. This is an alternative to the data option for the second parameter that allows binary content to be specified for the body of the POST.

Required Privileges

http://marklogic.com/xdmp/privileges/xdmp-http-post

Usage Notes

The http functions only operate on URIs that use the http or https schemes; specifying a URI that does not begin with http:// or https:// throws an exception.

If an http function times out, it throws a socket received exception (SVC-SOCRECV).

The function returns a sequence containing the request response. If the reponse is multi-part, then the sequence contains multiple items.

If you expect the request body from this http function to be processed by another application (via a web service, for example), you should specify a content-type header. If no content-type header is specified, the content type defaults to text/plain.

Example

xdmp:http-post("http://www.my.com/document.xhtml",
     <options xmlns="xdmp:http">
       <authentication method="basic">
         <username>myname</username>
         <password>mypassword</password>
       </authentication>
     </options>)
=> the response from the server as well as the specified document

Example

(: Use xdmp:quote to encode the XML as a string
   because the <data> options element is a string :)
let $payload := xdmp:quote(
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'
  xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
  xmlns:SOAP-ENC='http://schemas.xmlsoap.org/soap/encoding/'
  xmlns:my='urn:MyConnection'>
 <SOAP-ENV:Body>
   <my:LogOn>
     <my:User>user</my:User>
     <my:Password>pass</my:Password>
     <my:Ticket>abc123</my:Ticket>
     <my:newData>1234</my:newData>
   </my:LogOn>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
)
return
xdmp:http-post("http://www.my.com/document.xhtml",
     <options xmlns="xdmp:http">
       <authentication method="basic">
         <username>myname</username>
         <password>mypassword</password>
       </authentication>
       <data>{$payload}</data>
       <headers>
         <content-type>application/xml</content-type>
       </headers>
     </options>)
=> the response from the server as well as the specified document

Example

xquery version "1.0-ml";

let $payload := 
 '{ "user-name": "joe",
       "password": "cool"
       ,"role": [ "rest-reader" , "rest-writer" ]
  }' 
return
xdmp:http-post("http://localhost:8002/manage/v2/users?format=json",
     <options xmlns="xdmp:http">
       <authentication method="digest">
         <username>myname</username>
         <password>mypassword</password>
       </authentication>
       <headers>
         <content-type>application/json</content-type>
       </headers>
       <data>{$payload}</data>
     </options>)
=> the response from the server, and in this example, the new user (joe) is 
   created

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