Sending outbound business rules as curl

Tom23
Tera Expert

Hi guys,

So after the kind help given in https://community.servicenow.com/community?id=community_question&sys_id=947e0048dbb7d740e0e80b55ca96..., I'm now having different problems.

I'm trying to send a POST Rest message to the Jenkins API. I can reach the service ok but Jenkins is expecting an input in the following format:

curl -X POST http://[username:password@jenkins.******.co.uk:8080/job/[jobinfo]/build --data-urlencode json=‘{“parameter”: [{“name”:“UserName01”, “value”:“*******”}, {“name”:“BundleId01”, “value”:“******”}]}’

My business rule is looking like the below:

(function executeRule(current, previous /*null when async*/) {

try {
var r = new sn_ws.RESTMessageV2('Jenkins - Submit Workspace Request', 'POST');

var para = '--data-urlencode json=';
var paramid = {"name":"BundleId01", "value":"******};
var body = para + '{"parameter": [{"name":"UserName01", "value":"******"} + paramid]}';

var bodyText = JSON.stringify(body);
r.setRequestBody(bodyText);
gs.log('RESTLOG: ' + r.getRequestBody);

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log(response.getBody());
gs.log('RESPONSELOG: ' + response.getBody());
}
catch(ex) {
var message = ex.getMessage();
gs.log('MESSAGELOG: ' + message);
}

Message body from the outbound http message logs looks like this;

"--data-urlencode json={\"parameter\": [{\"name\":\"UserName01\", \"value\":\"XXXXXX\"} + paramid]}"

On the Jenkins side, this is being seen in the logs:

Aug 07, 2018 2:07:28 PM WARNING hudson.init.impl.InstallUncaughtExceptionHandler lambda$init$0
null
javax.servlet.ServletException: This page expects a form submission but had only {}

Can anyone please advise where I'm going wrong?

 

1 ACCEPTED SOLUTION

Thanks Simon. The issue did end up being on that side - essentially what we needed to do was to set the headers like this: 

 

find_real_file.png

Then we had to set the request body like so:

r.setRequestBody("json={\"parameter\":[{\"name\":\"**VARIABLE NAME**\" , \"value\":\"**VARIABLE VALUE**"\},{\"name\":\"**VARIABLE NAME**\" , \"value\":\"**VARIABLE VALUE**"\}]}");

 

Taking it one step further, we were able to dynamically change this content based on the ticket buy using \""+**current.FIELD_NAME**+"\"\ in place of variable values. In this way, we were able to get the variables passed across in the way Jenkins was expecting. It may will be specific to the instance of Jenkins we were using! Thanks again for the assist!

 

View solution in original post

4 REPLIES 4

simonbergstedt
Tera Guru

Dont think Jenkins requires you to use curl to send the message to jenkins, it's just one way of doing it. What does the configured REST-message look like for the POST-method? And what response do you get when you test it from there?

Thanks Simon - again!

So the Preview Script looks like this:

 

 try { 
 var r = new sn_ws.RESTMessageV2('Jenkins - Submit Workspace Request', 'POST');
 r.setStringParameterNoEscape('BundleId', 'wsb-2bs6k5lgn');
 r.setStringParameterNoEscape('Name', 'Service.Now.Test');
 r.setStringParameterNoEscape('UserName', 'servicenow.test');

//override authentication profile 
//authentication type ='basic'/ 'oauth2'
//r.setAuthentication(authentication type, profile name);

//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');

//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
}
catch(ex) {
 var message = ex.getMessage();
}

However, when we submit it (in this case I'm actually submitting this to a rest reflector we found on Share so we can see the actual body content,) and it's coming through blank.


find_real_file.png
I'm at a loss to explain why nothing's in the body in spite of the variables we've inserted?

Do you have a screenshot of this view in the Rest Message Configuration?

find_real_file.png

Thanks Simon. The issue did end up being on that side - essentially what we needed to do was to set the headers like this: 

 

find_real_file.png

Then we had to set the request body like so:

r.setRequestBody("json={\"parameter\":[{\"name\":\"**VARIABLE NAME**\" , \"value\":\"**VARIABLE VALUE**"\},{\"name\":\"**VARIABLE NAME**\" , \"value\":\"**VARIABLE VALUE**"\}]}");

 

Taking it one step further, we were able to dynamically change this content based on the ticket buy using \""+**current.FIELD_NAME**+"\"\ in place of variable values. In this way, we were able to get the variables passed across in the way Jenkins was expecting. It may will be specific to the instance of Jenkins we were using! Thanks again for the assist!