Difference between revisions of "Web Services First Steps"

From iDempiere en
 
(28 intermediate revisions by 5 users not shown)
Line 1: Line 1:
−
I started to experiment with Web Services and iDempiere and I found no good introduction. So I want want to document which steps I did to get into it.
+
'''Note (Release 14+):''' The SOAP Web Services bundle (<code>org.idempiere.webservice</code>) has been '''removed from iDempiere core''' and is now distributed as a separate optional plugin. Install it via the Extension Manager from [https://github.com/idempiere/idempiere-soap-webservices this repository] if your implementation requires SOAP. See the [[Migration_Notes|Migration Notes]] for upgrade instructions. Implementations using only the [[REST_Web_Services|REST API]] are unaffected.
  
−
== Preconditions ==
+
----
  
−
At first you have to be sure that the webservices plugin is installed and active. That should be the case in a standard iDempiere installation. The simplest test is to use https://localhost:8080/ADInterface/services and see if you get an answer. That should open a page that describes the two kinds of webservices and their methods.
+
I started to experiment with Web Services and iDempiere but I could not find a good introduction, so I documented the steps I took to get into it. Here you can see how to create a simple read-only Web Service to retrieve Business Partner details to get you started.
  
−
Standard webservices describe themselves as a so-called wsdl file. You can see a link to the wsdl files at the right. For example https://localhost:8080/ADInterface/services/ModelADService?wsdl is the wsdl file for the Model Oriented Service Interface.
+
= Pre-requisites =
 +
Before you start, make sure that the web services plugin is installed and active (which should be the case with a standard iDempiere installation) by browsing to:
 +
  http://localhost:8080/ADInterface/services
  
 +
The server name is ''localhost'' if you installed iDempiere locally, otherwise replace localhost with the name of your server if not.
  
−
== Create a request file with SoapUI ==
+
You should see a page showing two kinds of SOAP services and their methods, namely CompositeService and ModelADService, as well as a RESTful service:
  
−
To start a web request I need a file that describes my request. For my first steps it was the easiest way to use SoapUI (http://www.soapui.org) to create that.
+
[[File:SOAP Web Services.png|border]]
  
−
I pressed the "SOAP" button in the toolbar to create a new SOAP project. Then I set a name and gave the link to the wsdl file (http://localhost:8080/ADInterface/services/ModelADService?wsdl). Now SoapUI creates magically examples for all the methods.
+
(Note that the name of the server has been edited out in the example above, hence the blank spaces.)
  
−
Remember to press the "Save All" button to save your project files. That is not done automatically.
+
Standard SOAP web services are described by a Web Service Definition Language (WSDL) file which you can see in the links above on the right. For example, the WSDL file for the Model Oriented Service Interface is:
 +
  http://localhost:8080/ADInterface/services/ModelADService?wsdl
  
−
I did not find yet an alternative to SoapUI to create the example files. If you need that you should be able to write one using [[https://github.com/reficio/soap-ws soap-ws]]. If you understand how SOAP works it should be not so hard to just read the WSDL and create the xml manually.
+
You use the Web Service by posting a SOAP request to the URL. The SOAP request is a WSDL file of XML statements that describes the location of the service and the data to be passed in messages for particular operations.
  
 +
= Create the Web Service to access iDempiere =
 +
The first step to use a Web Service in iDempiere is to create an entry in the Application Dictionary and configure it with the name of the table that contains the data and the roles that have access to the service, amongst other things.
  
−
== Fill the Login part of the Request ==
+
For this example, log in as SuperUser/GardenAdmin to create a web service to query a list of Business Partners, as follows:
 +
* Open the window ''Web Service Security''
 +
* Create a new entry to describe the Web Service with the following:
 +
** '''Search Key:''' TestWebservices (the Search Key is important because it is the name that you use in your request)
 +
** '''Name:''' TestWebservices (the same as the Search Key in this example, but you can be more creative)
 +
** '''Web Service:''' Model Oriented Web Services (this is easier to start with than a Composite service)
 +
** '''Web Service Method:''' Model Oriented Web Services_Query Data (choose from the dropdown list)
 +
** '''Table:''' C_BPartner_Business Partner  (choose from the dropdown list)
 +
* Save the entry
  
−
At first your web service client should be able to login. For that you have to fill the ADLoginRequest part with the same credentials you fill when you log in manually. You can use this example as a reference. I used Client 11 (GardenWorld), Role 102 (GardenAdmin), Org 11 (HQ) and Warehouse 103 (HQ). For a real-world usage of Web Services you should better use a special role (not Admin). So you can exactly adjust which parts of iDempiere are available to the network access.
+
In the ''Web Service Parameters'' Tab below, add two parameters:
 +
* '''Parameter Name:''' TableName and '''Parameter Type:''' Free
 +
* '''Parameter Name:''' RecordID and '''Parameter Type:''' Free
  
−
  <_0:ADLoginRequest>
+
(Note: you can use constant values instead if you want to experiment further, such as TableName ''C_BPartner'' and RecordID ''50003'' for "Chemical, Inc.")
−
    <_0:user>SuperUser</_0:user>
 
−
    <_0:pass>System</_0:pass>
 
−
    <_0:lang>en_US</_0:lang>
 
−
    <_0:ClientID>11</_0:ClientID>
 
−
    <_0:RoleID>102</_0:RoleID>
 
−
    <_0:OrgID>11</_0:OrgID>
 
−
    <_0:WarehouseID>103</_0:WarehouseID>
 
−
    <_0:stage>?</_0:stage>
 
−
  </_0:ADLoginRequest>
 
−
 
 
−
 
 
−
== Configure a Web Service in iDempiere ==
 
  
−
To use a Webservice you first have to configure it. For that you log in as GardenAdmin and open the window "Web Services Security". Let us create a web service to query a list of Business Partners.
+
In the ''Web Service Field Output'' tab, configure the output field(s). You can add as many columns as you like but virtual columns do not work here (which may be a bug):
 +
* '''Column:''' Name_Name
  
−
You create a new Record. The Search Key is important. That is the name that you use in your Request. Let's call it "TestWebservice". The Service you use should be "Model Oriented WebServices" (for the beginning that is the easiest to start with) and the Method can be "Query Data" for this example. As the Table you choose "C_BPartner".
+
Lastly, in the ''Web Service Access'' tab add the Roles that need access to the Web Service:
 +
* '''Role:''' GardenWorld Admin (choose from the dropdownlist)
  
−
In the Tab for Parameters I added two parameters:
+
(Note: the username and role must be specified in the WSDL file when you send the web service request.)
  
−
* "TableName" as a free parameter (or with a constant value of C_BPartner)
+
= Create the WSDL request file =
−
* "RecordID" as a free parameter (or with a constant value of 50003 (that is the record id of "Chemical, Inc.") if you want to experiment further)
+
The next step is to create the WSDL file that contains the XML that describes the request. This is a text file in XML format that you will send to the SOAP Web Service as the payload of an HTTP request.
  
−
I am not sure if and why these parameters are needed. Especially the RecordID seems senseless for me in a query. But the wsdl says they are needed so I configured them.
+
You can create it manually from the command line ([[Web_Services_First_Steps#Create_the_WSDL_file_manually|see below]]), or you can use the [http://www.soapui.org SoapUI testing tool] to create it if you prefer not to use the command line.
 +
== Create and send the WSDL file using SoapUI ==
 +
If you prefer to use SoapUI, install it as follows:
 +
* Download the executable from [https://www.soapui.org/downloads/soapui.html here]
 +
* Make it executable (chmod a+x)
 +
* Run the installer
  
−
In the Tab "Web Services Field Output" I configured one output field (the name). You can add as many columns as you like. Virtual columns do not work here (That may be a bug).
+
Once SoapUI has been installed:
 +
* Start the application
 +
* Click on the "SOAP" button in the toolbar to create a new SOAP project
 +
* Give it a name
 +
* Enter the link to the WSDL: http://localhost:8080/ADInterface/services/ModelADService?wsdl
  
−
In the "Web Service Access" Tab I added the Role that needs Access (GardenAdmin in my example - but you should use a separate role for a production system).
+
Now SoapUI magically creates examples for all the methods. Remember to press the "Save All" button to save your project files. It is not done automatically.
  
 +
Use the sample data in the WSDL file ''request.xml'' described in the next section to do a simple query.
  
−
== Use my Web Service with a request file ==
+
I did not find an alternative to SoapUI, but you could probably write your own using [https://github.com/reficio/soap-ws this soap-ws Java library].
  
−
You enter the Search Key String of the Configuration we just created as the ServiceType in the xml request file. Then you add the needed parameter. The xml example that SoapUI created is useful to see all possible and needed parameters. To try out the request you can click the green arrow button in the Source window of SoapUI. You see the xml result at the left. There you can see a list of Business Partners or you get an error message that (hopefully) explains what you did wrong.
+
==Create the WSDL file manually==
−
 
+
The WSDL file is just a text file in XML format, so you can create it manually with a text editor. Create a text file ''request.xml'' in a convenient location with the following content:
−
My first successful request is this file:
 
  
 
   <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
 
   <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
Line 76: Line 91:
 
             <_0:OrgID>11</_0:OrgID>
 
             <_0:OrgID>11</_0:OrgID>
 
             <_0:WarehouseID>103</_0:WarehouseID>
 
             <_0:WarehouseID>103</_0:WarehouseID>
−
             <_0:stage>?</_0:stage>
+
             <_0:stage>0</_0:stage>
 
           </_0:ADLoginRequest>
 
           </_0:ADLoginRequest>
 
         </_0:ModelCRUDRequest>
 
         </_0:ModelCRUDRequest>
Line 82: Line 97:
 
     </soapenv:Body>
 
     </soapenv:Body>
 
   </soapenv:Envelope>
 
   </soapenv:Envelope>
 +
 +
=== The query data ===
 +
The WSDL query starts with the details of the Web Service that you configured above, ''TestWebservices'' in this example, and the name of the table with the data, ''C_BPartner''.
 +
=== The login data ===
 +
Your web service client must be able to login, so fill the ADLoginRequest part of the WSDL file with the same credentials you enter when you log in manually. In the example above I used:
 +
* '''user:''' Superuser
 +
* '''pass:''' System
 +
* '''ClientID:''' 11 (GardenWorld)
 +
* '''RoleID:''' 102 (GardenAdmin)
 +
* '''OrgID:''' 11 (HQ) and
 +
* '''WarehouseID:''' 103 (HQ)
 +
* '''stage:''' 0 (stage is not actually used, but you will get an error if you leave this out)
 +
 +
The ADLoginRequest looks like this:
 +
  <_0:ADLoginRequest>
 +
    <_0:user>SuperUser</_0:user>
 +
    <_0:pass>System</_0:pass>
 +
    <_0:lang>en_US</_0:lang>
 +
    <_0:ClientID>11</_0:ClientID>
 +
    <_0:RoleID>102</_0:RoleID>
 +
    <_0:OrgID>11</_0:OrgID>
 +
    <_0:WarehouseID>103</_0:WarehouseID>
 +
    <_0:stage>0</_0:stage>
 +
  </_0:ADLoginRequest>
 +
 +
== Sending the Web Service Request ==
 +
=== Web service request using curl ===
 +
You can use the standard curl utility to send and receive the HTTP request. You can get curl from [https://curl.haxx.se/download.html here] if it is not already installed.
 +
 +
Send the HTTP request with the SOAP details in the WSDL file as follows:
 +
 +
  curl --header "Content-Type: text/xml; charset=utf-8" --data @request.xml http://localhost:8080/ADInterface/services/ModelADService
 +
 +
The --data parameter ''@request.xml'' is the name of the XML file that describes your SOAP request (the ''@'' symbol tells curl that the data must be read from a file).
 +
 +
=== Web service request using wget ===
 +
You can also use wget to send the HTTP POST request and save the result to an XML file:
 +
 +
  wget http://localhost:8080/ADInterface/services/ModelADService --post-file=request.xml -O testresult.xml
 +
 +
You can use xmllint to format it if you want (or use any other XML formatting tool)
 +
 +
  xmllint --format testresult.xml
  
 
And the result is:
 
And the result is:
Line 126: Line 184:
 
   </soap:Envelope>
 
   </soap:Envelope>
  
−
== Use the request file from the console ==
+
If you want to add example code that you used feel free to add or link it here. :-) --[[User:TBayen|TBayen]] ([[User talk:TBayen|talk]]) 14:31, 8 August 2015 (CEST)
−
 
 
−
My next question was: How can I do that without using the SoapUI interface? That is easy to do. The SOAP Request is a simple HTTP POST request. I use wget to send it and save the result to an xml file:
 
−
 
 
−
  wget https://localhost:8080/ADInterface/services/ModelADService --post-file=testrequest.xml -O testresult.xml
 
−
 
 
−
You can use xmllint to format it if you want (or use any other xml formatting tool)
 
−
 
 
−
  xmllint --format testresult.xml
 
−
 
 
−
Now I am free to create the request file with whatever tool I want or use code in a language of my choice to handle the request and the result. That is not the scope of this article. If you want to add example code that you used feel free to add or link it here. :-) --[[User:TBayen|TBayen]] ([[User talk:TBayen|talk]]) 14:31, 8 August 2015 (CEST)
 
−
 
 
  
 
[[Category:Web Services]]
 
[[Category:Web Services]]
 +
[[Category:Development‏‎]]
 +
[[Category:Explanation]]
 +
[[Category:Developer documentation‏‎]]
 +
[[Category:NeedsToBeUpdated]]

Latest revision as of 13:20, 22 April 2026

Note (Release 14+): The SOAP Web Services bundle (org.idempiere.webservice) has been removed from iDempiere core and is now distributed as a separate optional plugin. Install it via the Extension Manager from this repository if your implementation requires SOAP. See the Migration Notes for upgrade instructions. Implementations using only the REST API are unaffected.


I started to experiment with Web Services and iDempiere but I could not find a good introduction, so I documented the steps I took to get into it. Here you can see how to create a simple read-only Web Service to retrieve Business Partner details to get you started.

Pre-requisites

Before you start, make sure that the web services plugin is installed and active (which should be the case with a standard iDempiere installation) by browsing to:

 http://localhost:8080/ADInterface/services

The server name is localhost if you installed iDempiere locally, otherwise replace localhost with the name of your server if not.

You should see a page showing two kinds of SOAP services and their methods, namely CompositeService and ModelADService, as well as a RESTful service:

SOAP Web Services.png

(Note that the name of the server has been edited out in the example above, hence the blank spaces.)

Standard SOAP web services are described by a Web Service Definition Language (WSDL) file which you can see in the links above on the right. For example, the WSDL file for the Model Oriented Service Interface is:

 http://localhost:8080/ADInterface/services/ModelADService?wsdl

You use the Web Service by posting a SOAP request to the URL. The SOAP request is a WSDL file of XML statements that describes the location of the service and the data to be passed in messages for particular operations.

Create the Web Service to access iDempiere

The first step to use a Web Service in iDempiere is to create an entry in the Application Dictionary and configure it with the name of the table that contains the data and the roles that have access to the service, amongst other things.

For this example, log in as SuperUser/GardenAdmin to create a web service to query a list of Business Partners, as follows:

  • Open the window Web Service Security
  • Create a new entry to describe the Web Service with the following:
    • Search Key: TestWebservices (the Search Key is important because it is the name that you use in your request)
    • Name: TestWebservices (the same as the Search Key in this example, but you can be more creative)
    • Web Service: Model Oriented Web Services (this is easier to start with than a Composite service)
    • Web Service Method: Model Oriented Web Services_Query Data (choose from the dropdown list)
    • Table: C_BPartner_Business Partner (choose from the dropdown list)
  • Save the entry

In the Web Service Parameters Tab below, add two parameters:

  • Parameter Name: TableName and Parameter Type: Free
  • Parameter Name: RecordID and Parameter Type: Free

(Note: you can use constant values instead if you want to experiment further, such as TableName C_BPartner and RecordID 50003 for "Chemical, Inc.")

In the Web Service Field Output tab, configure the output field(s). You can add as many columns as you like but virtual columns do not work here (which may be a bug):

  • Column: Name_Name

Lastly, in the Web Service Access tab add the Roles that need access to the Web Service:

  • Role: GardenWorld Admin (choose from the dropdownlist)

(Note: the username and role must be specified in the WSDL file when you send the web service request.)

Create the WSDL request file

The next step is to create the WSDL file that contains the XML that describes the request. This is a text file in XML format that you will send to the SOAP Web Service as the payload of an HTTP request.

You can create it manually from the command line (see below), or you can use the SoapUI testing tool to create it if you prefer not to use the command line.

Create and send the WSDL file using SoapUI

If you prefer to use SoapUI, install it as follows:

  • Download the executable from here
  • Make it executable (chmod a+x)
  • Run the installer

Once SoapUI has been installed:

Now SoapUI magically creates examples for all the methods. Remember to press the "Save All" button to save your project files. It is not done automatically.

Use the sample data in the WSDL file request.xml described in the next section to do a simple query.

I did not find an alternative to SoapUI, but you could probably write your own using this soap-ws Java library.

Create the WSDL file manually

The WSDL file is just a text file in XML format, so you can create it manually with a text editor. Create a text file request.xml in a convenient location with the following content:

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
   <soapenv:Header/>
   <soapenv:Body>
     <_0:queryData>
       <_0:ModelCRUDRequest>
         <_0:ModelCRUD>
           <_0:serviceType>TestWebservices</_0:serviceType>
           <_0:TableName>C_BPartner</_0:TableName>
         </_0:ModelCRUD>
         <_0:ADLoginRequest>
           <_0:user>SuperUser</_0:user>
           <_0:pass>System</_0:pass>
           <_0:lang>en_US</_0:lang>
           <_0:ClientID>11</_0:ClientID>
           <_0:RoleID>102</_0:RoleID>
           <_0:OrgID>11</_0:OrgID>
           <_0:WarehouseID>103</_0:WarehouseID>
           <_0:stage>0</_0:stage>
         </_0:ADLoginRequest>
       </_0:ModelCRUDRequest>
     </_0:queryData>
   </soapenv:Body>
 </soapenv:Envelope>

The query data

The WSDL query starts with the details of the Web Service that you configured above, TestWebservices in this example, and the name of the table with the data, C_BPartner.

The login data

Your web service client must be able to login, so fill the ADLoginRequest part of the WSDL file with the same credentials you enter when you log in manually. In the example above I used:

  • user: Superuser
  • pass: System
  • ClientID: 11 (GardenWorld)
  • RoleID: 102 (GardenAdmin)
  • OrgID: 11 (HQ) and
  • WarehouseID: 103 (HQ)
  • stage: 0 (stage is not actually used, but you will get an error if you leave this out)

The ADLoginRequest looks like this:

 <_0:ADLoginRequest>
   <_0:user>SuperUser</_0:user>
   <_0:pass>System</_0:pass>
   <_0:lang>en_US</_0:lang>
   <_0:ClientID>11</_0:ClientID>
   <_0:RoleID>102</_0:RoleID>
   <_0:OrgID>11</_0:OrgID>
   <_0:WarehouseID>103</_0:WarehouseID>
   <_0:stage>0</_0:stage>
 </_0:ADLoginRequest>

Sending the Web Service Request

Web service request using curl

You can use the standard curl utility to send and receive the HTTP request. You can get curl from here if it is not already installed.

Send the HTTP request with the SOAP details in the WSDL file as follows:

 curl --header "Content-Type: text/xml; charset=utf-8" --data @request.xml http://localhost:8080/ADInterface/services/ModelADService 

The --data parameter @request.xml is the name of the XML file that describes your SOAP request (the @ symbol tells curl that the data must be read from a file).

Web service request using wget

You can also use wget to send the HTTP POST request and save the result to an XML file:

 wget http://localhost:8080/ADInterface/services/ModelADService --post-file=request.xml -O testresult.xml

You can use xmllint to format it if you want (or use any other XML formatting tool)

 xmllint --format testresult.xml

And the result is:

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
     <ns1:queryDataResponse xmlns:ns1="http://idempiere.org/ADInterface/1_0">
        <WindowTabData NumRows="17" TotalRows="17" StartRow="0" xmlns="http://idempiere.org/ADInterface/1_0">
           <DataSet>
              <DataRow>
                 <field column="Name">
                    <val>Chemical Product, inc</val>
                 </field>
              </DataRow>
              <DataRow>
                 <field column="Name">
                    <val>Chrome, Inc</val>
                 </field>
              </DataRow>
              <DataRow>
                 <field column="Name">
                    <val>Color, Inc</val>
                 </field>
              </DataRow>
              <DataRow>
                 <field column="Name">
                    <val>C&W Construction</val>
                 </field>
              </DataRow>
 
              ...
 
              <DataRow>
                 <field column="Name">
                    <val>Wood, Inc</val>
                 </field>
              </DataRow>
           </DataSet>
           <RowCount>17</RowCount>
           <Success>true</Success>
        </WindowTabData>
     </ns1:queryDataResponse>
  </soap:Body>
 </soap:Envelope>

If you want to add example code that you used feel free to add or link it here. :-) --TBayen (talk) 14:31, 8 August 2015 (CEST)

Cookies help us deliver our services. By using our services, you agree to our use of cookies.