使用Nodejs向Jenkins发出POST请求

时间:2018-03-12 01:30:39

标签: node.js jenkins

我在使用NodeJS成功获得GET请求后发出了POST请求,我从Jenkins成功获得了碎屑,但我总是从Jenkins获得相同的响应"请求中没有包含有效的crumb& #34;,有人可以帮助我,我可能会在这里错过它吗?

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const request = require('request');

//Here we are configuring express to use body-parser as middle-ware.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.get('/',function(req,res){
    res.status('404').send('Not Found');
});

app.post('/process',function(req,res){

// Set the headers
var headers = {
    '--user':       'username',
    '--password':     'password'
}

// Configure the request
var options = {
    url: 'http://10.20.1.195:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)',
    method: 'GET',
    headers: headers
}

// Start the request
request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        var crumb = body; //Jenkins-Crumb:aa7d8b4c316415779703a04a99b85c50
        var headers = { crumb }
        // Configure the request
        var options = {
            url: 'http://localhost:8080/job/Nightwatch/buildWithParameters',
            method: 'POST',
            headers: headers,
            qs: {'TOKEN': 'special-token', 'PROJECT': req.body.project, 'CATEGORY': req.body.category, 'URL': req.body.staginUrl}
        }

        // Start the request
        request(options, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                // Print out the response body
                console.log(body)
            } else {
                console.log(response.statusMessage)
            }
        })

    } else {
        console.log('Could not get crumb from Jenkins! Check NodeJS Server')
    }
})

res.end('Complete');
});

2 个答案:

答案 0 :(得分:1)

包括Auth Token(从Jenkins的“安全性”选项卡内部生成)一起工作。将其包括为基本身份验证,即userid和password = token。应该可以。

答案 1 :(得分:0)

请查看此发行票。

https://github.com/spinnaker/spinnaker/issues/2067

这将解决问题。

相关问题