Search code examples
web-servicesxquerymarklogic

consuming an external web service using marklogic


Having a difficult time. Finding examples on, How to access an external web service using marklogic? (maybe my search terms are wrong? i also tried xdmp:http-get, xdmp:http-post, post http request, mash-up, orchestrate).

I first need to understand, How difficult (hopefully easy) it will be for me to write a script in MarkLogic to access one external (non-ML) web service and display the response before I proceed with combining results from 3 different web services (is the correct term for this mash-up?) in one page using ML.

An example using ML will be most appreciated. I have seen celsius to fahrenheit conversion examples, also stock quotes request and response but not in ML. I do not know how or where to start. Can you point me to right direction please. Eager to learn using ML for web services.

many thanks.


Solution

  • I'd say there are examples here: http://docs.marklogic.com/xdmp:http-post

    But for the sake of completeness, let me add these as well:

    Based on: http://www.w3schools.com/xml/tempconvert.asmx?op=FahrenheitToCelsius

    SOAP 1.1:

    let $envelop :=
      <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Body>
          <FahrenheitToCelsius xmlns="http://www.w3schools.com/xml/">
            <Fahrenheit>100</Fahrenheit>
          </FahrenheitToCelsius>
        </soap:Body>
      </soap:Envelope>
    return
      xdmp:http-post(
        "http://www.w3schools.com/xml/tempconvert.asmx",
        <options xmlns="xdmp:http">
          <headers>
            <Content-Type>text/xml; charset=utf-8</Content-Type>
            <SOAPAction>"http://www.w3schools.com/xml/FahrenheitToCelsius"</SOAPAction>
          </headers>
        </options>,
        $envelop
      )
    

    SOAP 1.2:

    let $envelop :=
      <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
        <soap12:Body>
          <FahrenheitToCelsius xmlns="http://www.w3schools.com/xml/">
            <Fahrenheit>100</Fahrenheit>
          </FahrenheitToCelsius>
        </soap12:Body>
      </soap12:Envelope>
    return
      xdmp:http-post(
        "http://www.w3schools.com/xml/tempconvert.asmx",
        <options xmlns="xdmp:http">
          <format xmlns="xdmp:document-get">xml</format>
          <headers>
            <Content-Type>application/soap+xml; charset=utf-8</Content-Type>
          </headers>
        </options>,
        $envelop
      )
    

    HTTP POST:

    let $body := text {
      string-join(
        ("Fahrenheit=" || encode-for-uri(string(100))),
        "&amp;"
      )
    }
    return
      xdmp:http-post(
        "http://www.w3schools.com/xml/tempconvert.asmx/FahrenheitToCelsius",
        <options xmlns="xdmp:http">
          <headers>
            <Content-Type>application/x-www-form-urlencoded</Content-Type>
          </headers>
        </options>,
        $body
      )
    

    HTH!