Difference between revisions of "Web Services First Steps"

From iDempiere en
(Grammar)
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
−
I started to experiment with Web Services and iDempiere but I found no introduction, so I documented the steps I took 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.
 +
 
 +
----
 +
 
 +
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 =
 
= Pre-requisites =
−
Make sure that the web services plugin is installed and active (which should be the case with a standard iDempiere installation) by browsing to:
+
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
 
   http://localhost:8080/ADInterface/services
  
Line 11: Line 15:
 
[[File:SOAP Web Services.png|border]]
 
[[File:SOAP Web Services.png|border]]
  
−
Note that the name of the server has been edited out in the example above, hence the blank spaces.
+
(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 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:
+
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
 
   http://localhost:8080/ADInterface/services/ModelADService?wsdl
  
−
It is this WSDL file with its XML statements that is sent to the web service endpoint to retrieve the data that we need.
+
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.
  
−
= Creating the Web Service Request =
+
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.
−
You need an XML file that describes the request to access a SOAP Web Service. You can do it manually from the command line ([[Web_Services_First_Steps#Web_service_request_using_curl|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 ==
−
== Web service request using SoapUI ==
+
If you prefer to use SoapUI, install it as follows:
−
Install SoapUI as follows:
 
 
* Download the executable from [https://www.soapui.org/downloads/soapui.html here]
 
* Download the executable from [https://www.soapui.org/downloads/soapui.html here]
 
* Make it executable (chmod a+x)
 
* Make it executable (chmod a+x)
Line 32: Line 65:
 
* Enter the link to the WSDL: http://localhost:8080/ADInterface/services/ModelADService?wsdl
 
* Enter the link to the WSDL: http://localhost:8080/ADInterface/services/ModelADService?wsdl
  
−
Now SoapUI magically creates examples for all the methods.
+
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.
−
 
 
−
Remember to press the "Save All" button to save your project files. That is not done automatically.
 
−
 
 
−
I did not find an alternative to SoapUI to create the example files but you could probably write your own using [[https://github.com/reficio/soap-ws this soap-ws Java library]].
 
−
 
 
−
== Web service request using curl ==
 
−
If you understand how SOAP works it is not difficult to read the WSDL and create the XML manually. You can then 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.
 
−
 
 
−
Create the request file and use curl to send the HTTP request with the SOAP details:
 
−
 
 
−
  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
 
−
 
 
−
= Creating the Web Service to access iDempiere =
 
−
== Configure the Web Service ==
 
−
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.
 
−
 
 
−
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".
 
  
−
In the Tab for Parameters I added two parameters:
+
Use the sample data in the WSDL file ''request.xml'' described in the next section to do a simple query.
  
−
* "TableName" as a free parameter (or with a constant value of C_BPartner)
+
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].
−
* "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)
 
  
−
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.
+
==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:
−
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).
 
−
 
 
−
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).
 
−
 
 
−
== Use the Web Service with a request file ==
 
−
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.
 
−
 
 
−
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 95: 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 101: 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 144: Line 183:
 
   </soap:Body>
 
   </soap:Body>
 
   </soap:Envelope>
 
   </soap:Envelope>
−
 
−
== The Login part of the Request ==
 
−
Your web service client must be able to login, so in the SOAP request, fill the ADLoginRequest part with the same credentials you fill when you log in manually.
 
−
 
−
In the example above 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 use a special role (not Admin) to specify which parts of iDempiere are available for web service access.
 
−
 
−
  <_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:stage>
 
−
  </_0:ADLoginRequest>
 
−
 
  
 
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)
 
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:Development‏‎]]
 +
[[Category:Explanation]]
 
[[Category:Developer documentation‏‎]]
 
[[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.