-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?

1
  • Are you talking about browser Javascript ? Or Node.js ? Commented Apr 6, 2020 at 8:21

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.