Skip to main content

Groovy Script - Call Web Service

Description: Sample Groovy Script to call a web service / API. To use any external URL in this script, it should be added to the Asite Whitelist URLs.

Context: Document

Document Type: File

Remark: Accessible in 'Post' task mode only.

Script:

def execute() {
// Get document details
DocumentVO document = documentService.getDocumentDetails();
String title = document.getDocTitle();
String revision = document.getRevision();

// API request
String apiUrl="https://<URL>/..."; // Make sure this URL is registered in Asite whitelist URLs
String apiRequestType = "POST"; // HTTP supported type

// Set request header parameters
Map<String, String> headerParameters = new HashMap<>();
headerParameters.put("Content-Type", "application/json"); // You can set any supported content type

// Set form parameters in form of key & value
Map<String, String> formParamMap = new HashMap<>();
formParamMap.put("docRevision", revision);
formParamMap.put("docTitle", title);
String body = ""; // You can set any body content in the form of String
HashMap<Object, Object> apiCallParameters = new HashMap<>();
apiCallParameters.put(IGroovyConstant.API_URL, apiUrl);
apiCallParameters.put(IGroovyConstant.API_REQUEST_TYPE, apiRequestType);
apiCallParameters.put(IGroovyConstant.HEADER_PARAM_MAP, headerParameters);
apiCallParameters.put(IGroovyConstant.FORM_PARAM_MAP, formParamMap);
apiCallParameters.put(IGroovyConstant.BODY, body);
Map<String, String> apiResponse = documentService.callExternalAPI(apiCallParameters);
if(! (apiResponse.get(IGroovyConstant.STATUS).equals("200")) ){
throw new Exception("API call failed. Response of API :: " + apiResponse.get(IGroovyConstant.DATA));
}
}
Did this answer your question?