Skip to main content

How to build a job using the REST API and Java?

Comments

10 comments

  • Bhaskar Polipilli

    Hi,

    I have tried the above solution and am getting the below exception

    java.io.IOException: Server returned HTTP response code: 403 for URL: http://localhost:8080/job/samplejob/build
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at com.itaf.majesco.jenkinsclient.JenkinsConnection.main(JenkinsConnection.java:25)

    I have installed jenkins on my machine, created a sample job "samplejob" and would like to build this job programatically.

    Below is the source which I am trying to execute:

    package com.sample.jenkinsclient;

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;

    import javax.xml.bind.DatatypeConverter;

    public class JenkinsConnection {

    public static void main(String[] args) {
    try {
    URL url = new URL ("http://localhost:8080/job/samplejob/build"); // Jenkins URL localhost:8080, job named 'test'
    String user = "admin"; // username
    String pass = "d546aece82de38a55c2f84bcdeb7a334"; // password or API token
    String authStr = user +":"+ pass;
    String encoding = DatatypeConverter.printBase64Binary(authStr.getBytes("utf-8"));

    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setRequestProperty("Authorization", "Basic " + encoding);
    InputStream content = connection.getInputStream();
    BufferedReader in =
    new BufferedReader (new InputStreamReader (content));
    String line;
    while ((line = in.readLine()) != null) {
    System.out.println(line);
    }
    } catch(Exception e) {
    e.printStackTrace();
    }

    }

    }

     

     

    Any help would be appreciated !!!

    0
  • Denys Digtiar

    Hi Bhaskar Polipilli,

    The HTTP response code is "Forbidden". Either Authentication or Authorization failed, i.e. either credentials are not right or the user is not permitted to build the "samolejob". You need to figure out which is the case and make appropriate adjustments.

    0
  • Mrityunjay Choudhary

    I am getting the same 403 error from Java code. When I Checked from Shell using cURL it threw the same 403 error. When researched a bit more, found out "Jenkins uses the "Prevent Cross Site Request Forgery exploits" security option (which it should), when you make a POST request, you have to send a CSRF protection token as an HTTP request header." Sorce: https://wiki.jenkins.io/display/JENKINS/Remote+access+API   (check CSRF Protection)

    As mentioned here, I generated crumb and added to the cURL, it worked perfectly fine. How can I add the crumb from Java code. 

    Please let know if anyone got this working with Java.

    0
  • Carlos Rodriguez Lopez

    You might get the crumb and build the job in the same call with something like this

    curl --user $USER:$APITOKEN -H $(curl --user $USER:$APITOKEN [-H "CRUB"] $SERVER/crumbIssuer/api/xml?xpath=concat\(//crumbRequestField,%22:%22,//crumb\))  $SERVER/job/hello-world-flow/build?token=codebase&cause=push
    0
  • Venkatesh Kona

    Hi,

     

    How to build a job with multiple parameters?

    In my scenario, i need to provide 4 parameters while build the job. Could you please provide your support asap.

     

    Thanks

    Venkatesh

    0
  • Sachin Vasdev

    Hey Venkatesh,

     

    I need to do the same thing did you find anything?

    0
  • Allan Burdajewicz

    Hi @Sachin @Venkatesh,

    The java example shows how to pass a single parameter. The parameter is defined at the line:

    String urlParams="paramA=123";

    If you wish to pass several parameters, you would need to add them as following:

    String urlParams="paramA=123&paramB=456";

    This is equivalent to the following REST API call:

    curl -XPOST --user developer:developer http://localhost:8080/job/test/buildWithParameters?paramA=123\&paramB=456
    0
  • Shirsak Sahoo

    Pass API token instead of password in the java API. It should work.

    0
  • Samrat Shaw

    You can have a look at the below article for programmatically retrieving Jenkins REST API Token.

    https://medium.com/@samratshaw/programmatically-retrieve-jenkins-rest-api-token-f2c3f0d69483

    0
  • Denys Digtiar

    Yes, since 2.107.1 the CSRF crumb is not required if REST API is made using the API Token instead of a password.

    0

Please sign in to leave a comment.

About CloudBees Support

Our Support Engineers are available to help with any questions or problems you may have with any of our products.