如何用JavaScript调用Jenkins API?[重复]

vmdwslir  于 12个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(184)

此问题在此处已有答案

How to make external HTTP requests with Node.js [closed](5个答案)
三年前就关门了。
你能告诉我如何用JavaScript调用Jenkins API吗?
下面是Python中的代码:

import requests

build = requests.post("http://YOUR_JENKINS_USER_ID:YOUR_API_TOKEN@YOUR_JENKINS_URL/job/YOUR_JENKINS_JOB/build?token=TokenName")

字符串
在Python中它运行得很好,你能告诉我如何在JavaScript中做同样的事情吗?

vfhzx4xs

vfhzx4xs1#

前端

如果你想从前端应用程序调用,代码看起来像下面这样:

async function makeRequest() {
    const url = "http://YOUR_JENKINS_USER_ID:YOUR_API_TOKEN@YOUR_JENKINS_URL/job/YOUR_JENKINS_JOB/build?token=TokenName"

    const res = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      }
    });
    const resJson = await res.json();

    return resJson;
}

字符串

Node.js应用

如果你想先从Node.js应用程序发出请求,你必须使用以下命令安装node-fetch

npm install node-fetch


然后你的代码看起来像这样:

const fetch = require('node-fetch');

async function makeRequest() {
    const url = "http://YOUR_JENKINS_USER_ID:YOUR_API_TOKEN@YOUR_JENKINS_URL/job/YOUR_JENKINS_JOB/build?token=TokenName"

    const res = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      }
    });
    const resJson = await res.json();

    return resJson;
}

相关问题