[+]
[+]
[+]
[-]
  
  
 [+]
 [+]
 [+]
  
  
  
 [+]
 [+]
 [+]
 [+]
 [+]
 [-]
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
 [+]
  
 [+]
  
Updated on 12/10/2019
Workflow Documentation
ScriptHttpClient
Direct link to topic in this publication:

PREVIOUS  |  NEXT

ScriptHttpClient

Quick Links


Table of Methods

[Back to top]

The following table lists the available methods for the ScriptHttpClient object:

Return Type Method Explanation
ScriptHttpResponse executeHttpDelete(String URI, Map<String, String> header) Executes the HTTP delete request
ScriptHttpResponse executeHttpGet(String URI, Map<String, String> header) Executes the HTTP get request 
ScriptHttpResponse executeHttpPost(String URL, String contentType, Map<String, String> header, String entity) Executes the HTTP post request
ScriptHttpResponse executeHttpPut(String URL, String contentType, Map<String, String> header, String entity)  Executes the HTTP put request
ScriptHttpResponse
executeHttpPut(String URL, String contentType, Map<String, String> header, String entity) 
Executes the HTTP patch request

HTTP POST Example

[Back to top]

The following example demonstrates how to create a user in an external application from Workflow, using the httpClient method:
// Workflow User Data
def userId = currentUser.getLoginName();
def addressId = currentCase.getCustomField('customer_address');
def city = currentCase.getCustomField('customer_city');
def state = currentCase.getCustomField('customer_state');
def zip = currentcase.getCustomField('customer_zip');

// data to send in POST request from Workflow to the external​ application
def ccURI = "http:/ /production.shippingapis.com/ShippingAPITest.dll"; // API that Workflow wants to access

// entity Workflow sends to the external application
def ccURL = "API=Verify&XML=<CreateUserRequest USERID=\"" + userId + 
"\"><AddressID=\"" + addressId + 
"\"><City>" + city + 
"</City><State>" + state + 
"</State><Zip>"+ zip + 
"</Zip></Address></CreateUserRequest>"; 

// contentType the external application expects in order to execute the POST request
def contentType = 'application/x-www-form-urlencoded'; 
def charset = "UTF-8"

// header attached to POST request
def httpParam = new java.util.HashMap(); 

httpParam.put('accept-charset', charset)
httpParam.put('Content-Language', 'en-US')

// Sending request to an external application
def httpClient = resp.createHttpClient();
def response = httpClient.executeHttpPost(ccURI, contentType, httpParam, ccURL);


Based on the example above, the external application will expect the request in the following format:

API=Verify&XML=<CreateUserRequest USERID="123456">
                  <Address ID="514 S. Magnolia St.">
                     <C
ity>Orlando</City>
                     <State>Florida</State>
                     <Zip>32806</Zip>
                   </Address>
               </CreateUserRequest>


You can now send a file over an HTTP POST from Workflow scripting. The following examples illustrate how to send a file over an HTTP POST from Workflow scripting.
Create script with the following code:

import groovy.json.JsonSlurper;
//1. SCRIPT DATA
// put your data here
def loginName = 'loginName';
def tenant = 'junit';
def contentType = 'application/json';
def contentFormType ='multipart/form-data';
def uri = 'http://localhost:8080/wpm/api/cases/'+currentCase.getSystemId()+'/attachments'; // replace with your installation url
def key = 'uploaded_file';

//2. GET/CREATE FILE
//this part needs to be modified by QA
// try also to get file from local file management, case attachment, file from file upload field, etc.
def file = resp.newFile('test.txt','bla bla');

//3. HTTP REQUEST HEADER
def user = resp.users.getUser(loginName);
def nameToken = user.getSystemId() + ":" + user.getWsSecurityToken();
def encoded = nameToken.bytes.encodeBase64().toString();
def authorizationHeader = "Basic " + encoded;
Map <String, String> headers = new HashMap();
headers.put('domain',tenant );
headers.put('Authorization', authorizationHeader);
headers.put('Accept', contentType);

//4. EXECUTIN HTTP REQUEST
def httpClient = resp.createHttpClient();
def response = httpClient.executeHttpPost(uri,contentFormType, headers, file, );
def result = new JsonSlurper().parseText(response.content); out << result;

// result should look something like this
/*
{
"name": "heigth1.png",
"download_link": "http://localhost:8080/wpm/downloadDocument.do?attachId=56608&tenantName=default",
"system_id": "56608",
"internal": true,
"company": {
"name": "CallidusCloud",
"system_id": "13",
"uri": "http://localhost:8080/wpm/api/companies/13"
},

"uploader": {
"login_name": "danica",
"first_name": "Danica",
"last_name": "Radisavljevic",
"user_type": {
"name": "QA",
"system_id": "qa"
},
"system_id": "danica",
"uri": "http://localhost:8080/wpm/api/users/danica"
}
}
*/

Put the script in action, after execution it should add the attachment in the script. Try to repeat steps 1 and 2 by using different files in script: -get file from local file management, -get existing attachment, -get file from file upload field, -get file from image upload field.


HTTP GET Example

[Back to top]

The following example illustrates how to get the status of a workflow case using the HTTP GET method and to move the current case to the designated status.

// get workflow case through HTTP GET
def httpClient = resp.createHttpClient();

// send API url for getting a case in Workflow
def response = httpClient.executeHttpGet('https:/ /test.webcomserver.com/wpm/api/cases/Task-sp-3');

// Workflow sends a JSON response, so it must be parsed to get the status
def slurper = new groovy.json.JsonSlurper();
def responseString = response.getContent().toString();
def json = slurper.parseText(responseString);

// check for errors
if(!json.isNull('error_code')) {
resp.fail("Case doesn't exist!")
}

// get status from JSON and set it as the new status of currentCase
def status = resp.getStatus(json.status.system_id); // system_id = described
currentCase.setStatus(status);
resp.update(currentCase);



The response for the workflow create case request may appear as follows:

{
"assignees":[
{
"first_name":"Tom",
"last_name":"Smith",
"login_name":"tsmith",
"system_id":"tsmith",
"uri":"http:/ /localhost:8080/wpm/api/users/tsmith",
"user_type":{
"name":"Sales and Marketing",
"system_id":"sales_and_marketing"
}
}
],
"closed":false,
"created_on":"09/10/13 14:50:49",
"creator":{
"first_name":"John",
"last_name":"Smith",
"login_name":"jsmith",
"system_id":"jsmith",
"uri":"http:/ /localhost:8080/wpm/api/users/jsmith",
"user_type":{
"name":"Sales and Marketing",
"system_id":"sales_and_marketing"
}
},
"status":{
"name":"Described",
"system_id":"described"
},
"system_id":"Task-sp-3",
"updated_on":"09/10/13 14:50:49",
"uri":"http:/ /localhost:8080/wpm/api/cases/Task-sp-3"
}


HTTP PATCH Example

[Back to top]

The following example illustrates how to make partial updates to a workflow case using the HTTP PATCH  method.

fetch('https://jsonplaceholder.typicode.com/posts/1', {
method: 'PATCH',
body: JSON.stringify({
title: 'foo'
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => console.log(json))

The response for the request may appear as follows:

// Output
{
id: 1,
title: 'foo',
body: '[...]',
userId: 1
}

Related Articles