Making Web Service Requests
Making web service requests is a common way to exchange data with other systems in an enterprise environment. You can use ajCallWsRest and ajCallWsSoap to make REST API call and SOAP API call in your AlchemyJ model respectively.
Invoking a REST API
In this example, we will call the customers endpoint with a GET request which returns a list of customers.
We will use ajCallWsRest to make the request.
ajCallWsRest(Url As String, [Http_method], [Body], [User_name], [Password], [Header], [Content_type], [Run_condition], [Run_by_function_point_only])
For a simple call in this example, we only need to define the following parameters:
Parameter | Value |
---|---|
Url | Put the Url of the REST endpoint here: http://localhost:8080/api/v1/customers |
Http_method | Put 1 here as we want to make a GET request. |
As ajCallWsRest might return a response message that is longer than the capacity of a single cell, highlight multiple cells on the same row, type in =ajCallWsRest(‘http://localhost:8080/api/v1/customers’,1) and press CTRL+SHIFT+ENTER to enter it as an array formula. The return data will span across multiple cells if it cannot fit into one cell.
{{"id":"1","customer_name":"IvanHui","customer_no":"1100001","staus":"A"},{"id":"2","customer_name":"ViviLeung","customer_no":"1100002"," staus":"A"},{"id":"3","customer_name":"MeryLi","customer_no":"1100003"," staus":"C"}}
Invoking a SOAP API
Similarly, ajCallWsSoap can be used to make a SOAP API call. In this example, we will make a POST request to a SOAP API.
We will use ajCallWsSoap to make the request.
ajCallWsSoap(Url As String, [Http_method], [Body], [User_name], [Password], [Header], [Content_type], [Run_condition], [Run_by_function_point_only])
For the simple call in this example, we only need to define the following parameters:
Parameter Value Url Put the url of the SOAP API here: http://localhost:8080/WebServices/CustomerList.asmx Http_method Put 0 here as we want to make a POST request. Body For a POST request, we shall define the message to be sent in the body. Below is the XML message we will be sending to the API. Put it in cell B2. <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <getCustomerbyID xmlns="http://WebXml.com.cn/"> <ID>110001</ID > </getCustomerbyID > </soap:Body> </soap:Envelope>
As ajCallWsSoap may return a response message that is longer than the capacity of one cell, highlight multiple cells on the same row, type in =ajCallWsSoap(‘http://localhost:8080/WebServices/CustomerList.asmx’,0,B2) and press CTRL+SHIFT+ENTER to enter it as an array formula. The return data will span across multiple cells if it cannot fit into one cell.
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> < getCustomerbyIDResponse xmlns="http://WebXml.com.cn/"> < getCustomerbyIDResult> <ID>110001</ID> <Name>IvanHui</Name> <Status>A</Status> </getCustomerbyIDResult> </getCustomerbyIDResponse> </soap:Body> </soap:Envelope>