Nav

To Consume a Web Service

Once your Web Service Consumer (WSC) connector configuration is created, you can start consuming SOAP operations from the configured service. (Note that if you have not created a Web Service Consumer configuration yet, you can refer to the Web Service Consumer Configuration Documentation).

The WSC connector contains a single operation, the Consume (wsc:consume) operation. Two important parameters of this operation are Operation (operation) and Message (wsc:message).

Here is the basic syntax of the Consume operation:

Basic Consume Operation Example

         
      
1
2
3
4
5
<wsc:consume config-ref="config" operation="clients-list">
    <wsc:message>
        <wsc:body>#[payload]</wsc:body>
    </wsc:message>
</wsc:consume>

If something goes wrong when dispatching the SOAP message, you will receive an WSC:CANNOT_DISPATCH error.

The Operation Parameter

The operation parameter defines which SOAP operation of the Web service to invoke. In design time, the operation parameter will define the input and output types for the consume operation. The types will change depending on which operation you choose.

The Message Parameter

The message parameter is a representation of the SOAP:ENVELOP that is composed of 3 elements: Body, Headers, and Attachments. All are optional.

Body

The body is the main part of the message. It contains the SOAP message that is intended for the ultimate endpoint of the SOAP message. This parameter is always an XML application-specific element.

The body element accepts embedded DataWeave scripts as values so that you can construct the XML request without having a side effect on the message or having to use multiple components to create the request. By default, the value of the message body is #[payload]. This means that by default it expects that the incoming payload is the XML entity that is ready to be shipped to the service.

Here is an example of a transformation inside the body element:

DataWeave expression inside the body parameter

          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<wsc:consume config-ref="config" operation="addClients">
    <wsc:message>
        <wsc:body>
        #[
        %dw 2.0
        output application/xml
        ns con http://service.soap.clients.namespace/
        ---
        con#clients: {
            client: {
                name: "Natasha",
                lastname: "Ferrari"
            },
            client: {
                name: "Lucas",
                lastname: "Barzola"
            }
        }]
        </wsc:body>
    </wsc:message>
</wsc:consume>

If the body is not a valid XML, or if the request cannot be created for some reason, you will get a WSC:BAD_REQUEST error.

Note that if no body content is provided, the Web Service Consumer will try to generate one. This will only work for cases where no XML entity is expected in the body.

Headers

The optional headers element contains application-specific information (like authentication, payment, and so on) about the SOAP message. Like the body element, this element is an XML entity, and it can embed a DataWeave script as its value, for example:

DataWeave expression inside the headers parameter

          
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<wsc:consume config-ref="config" operation="addClients">
    <wsc:message>
        <wsc:body>#[payload]</wsc:body>
        <wsc:headers>
          #[
          %dw 2.0
          output application/xml
          ns con http://service.soap.clients.namespace/
          ---
          "headers": {
              con#user: "NahuelMonin",
              con#pass: "%M4g\/iT0Rn4!"
          }]
        </wsc:header>
    </wsc:message>
</wsc:consume>

Attachments

The attachments element enables you to bind attachments to the SOAP message. To create attachments for transport over SOAP, you need to declare a DataWeave script in which each entry represents an attachment and its value provides the content of the attachment, for example:

DataWeave expression inside the attachments parameter

          
       
1
2
3
4
5
6
7
8
<wsc:consume config-ref="config" operation="addClients">
    <wsc:message>
        <wsc:body>#[payload]</wsc:body>
        <wsc:attachments>
          #[{ clientsJson: vars.jsonFile } ]
        </wsc:attachments>
    </wsc:message>
</wsc:consume>

The example declares a new attachment called clientsJson the content of which is stored in the variable called jsonFile. This variable could be set from a file:read operation, for example.

Output

The output of the Consume operation represents an incoming SOAP message that contains the same elements that the message parameter has, and you can access each part of it.

Here is an example:

DataWeave expression inside the body parameter

         
      
1
2
3
4
5
6
7
8
9
10
<flow name="output">
  <wsc:consume config-ref="config" operation="addClients">
      <wsc:message>
          <wsc:body>#[payload]</wsc:body>
      </wsc:message>
  </wsc:consume>
  <set-variable name="soap.body" value="#[payload.body]">
  <set-variable name="soap.auth.header" value="#[payload.headers.auth]">
  <set-variable name="soap.attachment.json" value="#[payload.attachments.json]">
</flow>

The example stores the content of the body in a new variable called soap.body. It stores a header called auth in a soap.auth.header variable, and it stores the content of an attachment called json in a variable called soap.attachment.json

Attributes

When consuming a Web service operation, you might be interested not only in response content but also in metadata of the underlying transport used to dispatch the messages. For example, when you use HTTP, attributes carry HTTP headers that are bound to the HTTP request (content-length, status, and so on).

The Web Service Consumer uses the Mule Message Attributes to access this information.