Rapid PL/SQL Web Service Client development (using soapUI and utl_http)

Lucas Jellema 7
0 0
Read Time:6 Minute, 7 Second

I have written about similar topics on several occasions. But now I feel an urgent need to do so again.  As with the use of the wonderful soapUI tool and a fairly well refactored piece of PL/SQL, you can create PL/SQL code calling into SOAP WebServices in no time at all. They can be external, maybe even public web service, or the front ends of internal services deployed to an Enterprise Service Bus or a BPEL engine. Thanks to standards such as WSDL, SOAP and XML it all works in the same way.

In this article I will show how I:

  • locate a temperature conversion service in the public domain
  • use soapUI to rapidly test call this service, meanwhile getting examples of the required request message structure as well as the returned response message structure
  • extend a piece of reusable PL/SQL code to call the service and process the response

This allows me to create a PL/SQL Function convertFahrenheitToCelcius( p_fahrenheit_temperature in number) without knowing the first thing about adding/subtracting 32 and multiplying/dividing by 9 and 5. In less than 7 minutes. And being in New Orleans where the weather report speaks of temperatures in the 80s and 90s, I have a serious need to do this conversion before picking my garb for the day.


1. locate a temperature conversion service in the public domain

I frequently have a problem to find a publicly available web service for any demo or article. Today, I came across a web site called http://seekda.com/. This site offers pointers to many different web services, including the one I needed: Convert Temperature (from Celsius to Fahrenheit and back). Its description is at: http://seekda.com/service_details?uri=http%3A%2F%2Fwww.webservicex.net%2FConvertTemperature.asmx%3Fwsdl .

With the WSDL location in my pocket, I can find out how to invoke this service. However, that can be quite a tricky job (for someone as unstructured as me). And there is an easier way to find out how to call this service:

2. use soapUI to rapidly test call this service, meanwhile getting examples of the required request message structure as well as the returned response message structure

I downloaded soapUI, an open source Web Service Testing tool. It can do quite a bit more than just testing by the way – or it has a pretty broad definition of the term testing. Anyway, after downloading soapUI, I ran it. I create a new WSDL project and feed it the URL of the WSDL for the ConvertTemperature Service:

Now even though soapUI can create the outline for my SOAP requests into the service, I still need to provide some of the values:

The question is: what are the values I can fill in, especially from the fromUnit and toUnit items. In the WSDL for the service, the document that I preferred not to look at myself, I quickly located the information needed:

The values for fromUnit and toUnit are degreeCelsius and degreeFahrenheit. Now I can complete the SOAP request message:

and press the run button to perform a call to the service. The response comes in looking like this:

So now I know exactly what SOAP message to send to the service:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"

and what response message to expect in return.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
      <ConvertTempResponse xmlns="http://www.webserviceX.NET/">

Time to create the PL/SQL code that will perform the call and expose the functionality as a ‘native’ PL/SQL function.

3. extend a piece of reusable PL/SQL code to call the service and process the response

We can first set up a nice generic PL/SQL function that will handle all the SOAP plumbing for us.

  create or replace function soap_call
  ( p_payload    in varchar2
  , p_target_url in varchar2
  , p_soap_action in varchar2 default 'process'
  ) return xmltype
    c_soap_envelope varchar2(250):= '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    l_soap_request varchar2(30000);
    l_soap_response varchar2(30000);
    http_req utl_http.req;
    http_resp utl_http.resp;
    l_soap_request := replace(c_soap_envelope, '**payload**', p_payload);
    http_req:= utl_http.begin_request
               ( p_target_url
               , 'POST'
               , 'HTTP/1.1'
    utl_http.set_header(http_req, 'Content-Type', 'text/xml');
    utl_http.set_header(http_req, 'Content-Length', length(l_soap_request));
    utl_http.set_header(http_req, 'SOAPAction', p_soap_action);
    utl_http.write_text(http_req, l_soap_request);
    -- the actual call to the service is made here
    http_resp:= utl_http.get_response(http_req);
    utl_http.read_text(http_resp, l_soap_response);

    -- only return the payload from the soap response - that is: the content of the body element in the SOAP envelope
    return XMLType.createXML( l_soap_response).extract( '/soap:Envelope/soap:Body/child::node()'
                     , 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"'

Note how this function has now Temperature Service specific elements – it is completely generic and can be used for calling any SOAP based Web Service.

Then we write the specific function for converting the temperature. This function sends the proper payload for the service we want to call and processes the returned response:

create or replace function convertFahrenheitToCelcius
( p_fahrenheit_temperature in number
) return number
  l_response_payload  XMLType;
  l_payload           varchar2(2000);
  l_payload_namespace varchar2(200);
  l_target_url        varchar2(200);
  l_payload_namespace := 'http://www.webserviceX.NET/';
  l_target_url        := 'http://www.webservicex.net/ConvertTemperature.asmx';
  l_payload          :=
    '  <web:ConvertTemp xmlns:web="http://www.webserviceX.NET/">
  l_response_payload := soap_call(l_payload, l_target_url, 'http://www.webserviceX.NET/ConvertTemp');
  return to_number(l_response_payload.extract('/ConvertTempResponse/ConvertTempResult/text()', 'xmlns="'|| l_payload_namespace||'"').getStringVal());

With the new function in place, we can write queries like this one – and get the temperature converted. Mind you – the performance of this particular function is not wildly terrific; we could certainly benefit from the 11g Result Cache feature for this one. Or of course learn how to do this calculation ourselves.


Search Engine for publicly available Web Services: http://seekda.com/

soapUI – Web Service Testing tool – http://www.soapui.org/

About Post Author

Lucas Jellema

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director and Oracle Developer Champion. Solution architect and developer on diverse areas including SQL, JavaScript, Kubernetes & Docker, Machine Learning, Java, SOA and microservices, events in various shapes and forms and many other things. Author of the Oracle Press book Oracle SOA Suite 12c Handbook. Frequent presenter on user groups and community events and conferences such as JavaOne, Oracle Code, CodeOne, NLJUG JFall and Oracle OpenWorld.
0 %
0 %
0 %
0 %
0 %
0 %

Average Rating

5 Star
4 Star
3 Star
2 Star
1 Star

7 thoughts on “Rapid PL/SQL Web Service Client development (using soapUI and utl_http)

  1. Indeed, you’ll have some trouble setting it up. It involves running s script (have to find out which one) and creating a synonym.
    I hope the following code snippet (not complete) example is a bit readable :
    l_service := UTL_DBWS.create_service (
    wsdl_document_location => URIFACTORY.getURI(l_wsdl_url),
    service_name => l_service_name);
    l_call := UTL_DBWS.create_call (
    service_handle => l_service,
    port_name => NULL,
    operation_name => l_operation_name);
    l_result := UTL_DBWS.invoke (
    call_handle => l_call,
    input_params => l_input_params);
    @Anton, I’ll send you the complete example.

  2. Hi Aino,
    Thanks for your suggestion. I have been looking at utl_dbws but I was not yet convinced of how easy it is to use. Resources on blogs and even the Oracle documentation have not convinced me. (I was looking for example at the Oracle Base Article: http://www.oracle-base.com/articles/10g/utl_dbws10g.php. Is your experience good? It seems like a lot of trouble to go through to set it up and use it- but perhaps with more complex service calls it is worth it.


  3. Hi Lucas,

    For interaction with soap webservices, utl_dbws is much more convenient. You interact with the service using the wsdl url, service name and operation name and don’t have tot worry about the actual interaction, including the soap request and response.

Comments are closed.

Next Post

News, questions and Rumours from the ODTUG conference - APEX, SQL Developer, BEA, WebCenter, Forms,...

Any conference with a large crowd of developers, passionate about their work and the tools they work with, is of course a breeding ground for speculations, rumours, frustrations, honest questions and subtle attempts at manipulation. The Oracle Development Tools User Group Conference – ODTUG 2008 Kaleidoscope – currently underway in […]
%d bloggers like this: