node-jenkins-api
node-jenkins-api copied to clipboard
Crumb support
I don't see any documentation around support for CRUMB. How to make it work for Jenkins that has CSRF protection configured
@RameshThangamuthu you can using the following code
quick & dirty
`
build: function(jobname, params, callback) {
var buildurl;
/*
Trigger Jenkins to build.
*/
if (typeof params === 'function') {
buildurl = build_url(BUILD, jobname);
callback = params;
} else {
buildurl = build_url(BUILDWITHPARAMS+"?"+qs.stringify(params), jobname)
}
/*
http://******/jenkins//job/springbootdemo/build/api/json
*/
var url = buildurl.replace('/job/'+jobname+'/build/api/json','')+'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)';
request({method: 'GET', url:url}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var crumb = body.split(':', 2)[1];
request({method: 'POST', url: buildurl ,headers:{'Jenkins-Crumb':crumb}}, function(error, response) {
if ( error || (response.statusCode !== 201 && response.statusCode !== 302) ) {
callback(error, response);
return;
}
/*
Return queue location of newly-created job as per
https://issues.jenkins-ci.org/browse/JENKINS-12827?focusedCommentId=201381#comment-201381
*/
var data = {
message: "job is executed",
location: response.headers["Location"] || response.headers["location"]
};
callback(null, data);
});
}else{
callback(error, response);
}
});
},
`
@galenzhao where should the code that you posted go? is a patch to the api library ?
Opened Pull Request #75, for this issue