Loading TOC...

xdmp:http-post

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

Summary

Sends the http POST request to the server.

Parameters
uri The URI to which the data is to be posted.
options The options node for this request. The default value is (). The node for the xdmp:http-post options must be in the xdmp:http namespace.

The xdmp:http-post options include the following option (in the xdmp:http namespace), in addition to the options documented with the xdmp:http-get $options parameter:

<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).

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>
       <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.