您能告诉我如何使用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中做同样的事情?
如果要从前端应用程序进行呼叫,代码将如下所示:
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-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;
}