-3

Could you tell me how to call jenkins api with javascript?

I have the code in python:

import requests

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

and in python it works perfectly fine, could you please tell me how to do the same in javascript?

joker20
  • 53
  • 2
  • 8

1 Answers1

1

Frontend

If you want to make a call from the frontend application the code would look like following:

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 Application

If you want to make request from Node.js application first you have to install node-fetch using following command:

npm install node-fetch

Then your code would look like following:

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;
}
ziishaned
  • 4,944
  • 3
  • 25
  • 32