Call WCF Services using HTTPClient or RESTClient objects

Prior to PowerBuilder 2017 R2, the SoapClient did not support calling WCF Services.

In PowerBuilder 2017 R2 and the latter versions, the HTTPClient object is available, through which you can call WCF Services.

In PowerBuilder 2019, the RESTClient object is strengthened, now you can also call the WCF Services.

How to use HTTPClient object to consume a WCF Service

a. To get the methods and parameters to consume the WCF Service.

Normally when you open a WCF Service website, you will get the following page:


However, you would still need the documentation provided by the WCF Service and also need to verify if the current WCF Service supports XML format parameters in order to consume this WCF using URL or through a client. If the current service provider has provided the online help, you can use the WCF Service address + "/help" to obtain the corresponding API descriptions and the parameters as follows:


Note: If helpEnabled is not set or is set to false in the Web.config of the current WCF Service provider, then the help page won’t display.


Let’s take the GetDataUsingDataContract method as an example. Click the corresponding post method to open a new link shown in the screenshot below:

There is the example request XML body for us to consume this API.


Before we write PowerScript to call this method, we can also verify if we can successfully access it using a third-party tool (such as Postman), and see if the current parameters are valid.

The example request XML body content we copy is as follows:

<CompositeType xmlns="http://schemas.datacontract.org/2004/07/WelcomeWcfService"> 
  <BoolValue>true</BoolValue> 
  <StringValue>String content</StringValue> 
</CompositeType>

We can test this API in Postman first as shown in the screenshot below:


In the example, we use Postman to verify this API.

Note: You should replace the API content with your actual API content after you copy the XML.

For example: In the GetDataUsingDataContract method of this example, the StringValue parameter should be changed from “String content” to “New Test”.

b. The PowerScript of using the HTTPClient object to call the API.

After you have verified the API using Postman, use the same protocol and content to implement it with the HTTPClient object from PowerBuilder.


Example code:

httpClient   lhc_client 
integer   li_ret, i , li_StatusCode 
String   ls_xml_request 
string   ls_url, ls_body, ls_ret, ls_data 
PBDOM_Builder   lpbdom_builder 
pbdom_document   lpbdom_doc 
string   ls_BoolValue, ls_StringValue 
ls_url ="http://localhost:61869/WelcomeService.svc/GetDataUsingDataContract" 
ls_xml_request ='<CompositeType xmlns="http://schemas.datacontract.org/2004/07/WelcomeWcfService">' + & 
 ' <BoolValue>true</BoolValue>' + & 
 ' <StringValue>New Test </StringValue>' + & 
'</CompositeType>' 
lhc_client = Create httpClient 
lhc_client.SetRequestHeader("Content-Type", "application/xml") 
lhc_client.SetRequestHeader("Content-Length",string(len(ls_xml_request) )) 
ls_data = "" 
li_ret = lhc_client.sendrequest("POST", ls_url,ls_xml_request ) 
li_StatusCode = lhc_client.GetResponseStatusCode() 
ls_ret = lhc_client.GetResponseStatusText( ) 
li_ret = lhc_client.getresponsebody(  ls_data) 
destroy lhc_client 
lpbdom_builder = Create PBDOM_Builder 
lpbdom_doc = lpbdom_builder.BuildFromString(ls_data) 
ls_BoolValue = lpbdom_doc.GetRootElement().GetChildElement( "BoolValue","", "http://schemas.datacontract.org/2004/07/WelcomeWcfService").GetText() 
ls_StringValue = lpbdom_doc.GetRootElement().GetChildElement( "StringValue","", "http://schemas.datacontract.org/2004/07/WelcomeWcfService").GetText() 
destroy lpbdom_builder

How to use the RESTClient object to consume a WCF Service.

a. To get the method and parameters to consume the WCF Service.

Normally when you open a WCF Service website, you will get the following page:


However, you would still need the documentation provided by the WCF Service and also need to verify if the current WCF Service supports JSON format parameters in order to consume this WCF using URL or through a client. If the current service provider has provided the online help, you can use the WCF Service address + "/help" to obtain the corresponding API descriptions, the parameters as follows:


Note: If helpEnabled is not set or is set to false in the Web.config of the current WCF Service provider, then the help page won’t display.


Let’s take the GetDataUsingDataContract method as an example. Click the corresponding post method to open a new link shown in the screenshot below:

There is the example request body for us to consume this API.


Note: When using the RESTClient object to access the GetDataUsingDataContract method, we have modified the returned value in WCF Service to support JSON instead of the previous default XML content. (The example in the previous HTTPClient object method uses XML format returned value.)

Before we write PowerScript to call this method, we can also verify if we can successfully access it using a third-party tool (such as Postman), and see if the current parameters are valid

The example request JSON body content we copy is as follows:

{ 
      "BoolValue":true, 
      "StringValue":"String content" 
}

We can test this API in Postman first as shown below:


Then we can see that the current code works correctly.

b. The PB code of using the RESTClient object to call the API.

After you have verified the API with Postman, you can use the same protocol and content to implement it with the RESTClient object from PowerBuilder.

Please note that the current code has been verified in PowerBuilder 2019 Build 2170.


Example code:

Integer                 li_return, li_StatusCode 
String                    ls_url, ls_ret, ls_data, ls_json 
String                    ls_Error, ls_value 
RestClient            lrc_RestClient 
JsonParser          ljp_JsonParser 
Boolean                                lb_flag 
ls_json = '{"BoolValue":true,       "StringValue":"String content "}' 
ls_url = "http://localhost:61869/WelcomeService.svc/GetDataUsingDataContract" 
lrc_RestClient = Create RestClient 
// Construct a POST request (supports all headers) 
lrc_RestClient.SetRequestHeader("Content-Type", "application/json;charset=UTF-8") 
li_return = lrc_RestClient.SendPostRequest(ls_url, ls_json, ls_data) 
// Check the return value 
if li_return = 1 then 
                li_StatusCode = lrc_RestClient.GetResponseStatusCode() 
                ls_ret = lrc_RestClient.GetResponseStatusText() 
                ljp_JsonParser = Create JsonParser 
                ls_Error = ljp_JsonParser.LoadString(ls_data) 
                if Len(ls_Error) = 0 then 
                                ls_value = ljp_JsonParser.GetItemString(ljp_JsonParser.GetRootItem(), "StringValue") 
                                lb_flag = ljp_JsonParser.GetItemBoolean(ljp_JsonParser.GetRootItem(), "BoolValue") 
                end if 
                Destroy ljp_JsonParser 
else 
                MessageBox("Error", "Failed to retrieve data.") 
end if 
Destroy lrc_RestClient