【问题标题】:Using Nodejs to make POST request to Jenkins使用 Nodejs 向 Jenkins 发出 POST 请求
【发布时间】:2018-08-19 23:36:32
【问题描述】:

在使用 NodeJS 成功获取 GET 请求后,我正在发出 POST 请求,我从 Jenkins 成功获取了面包屑,但我总是从 Jenkins 得到相同的响应“请求中没有包含有效的面包屑”,有人可以帮忙吗我在哪里可能会错过它?

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');
});

【问题讨论】:

    标签: node.js jenkins


    【解决方案1】:

    包括 Auth Token(从 Jenkins 安全选项卡内部生成)可以完成这项工作。将其包含为基本身份验证,即用户 ID 和密码 = 令牌。应该可以的。

    【讨论】:

      【解决方案2】:

      请查看此问题单。

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

      这会解决问题。

      【讨论】:

      • 我看过了,你到底在指出什么?我使用的是 NodeJs 而不是 wget、cURL、python 或 Ruby。你是在建议我关掉安全吗?我不想那样做。
      猜你喜欢
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多