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.
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.
Comments
10 comments
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 !!!
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.
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.
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
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
Hey Venkatesh,
I need to do the same thing did you find anything?
Hi @Sachin @Venkatesh,
The java example shows how to pass a single parameter. The parameter is defined at the line:
If you wish to pass several parameters, you would need to add them as following:
This is equivalent to the following REST API call:
Pass API token instead of password in the java API. It should work.
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
Yes, since 2.107.1 the CSRF crumb is not required if REST API is made using the API Token instead of a password.
Please sign in to leave a comment.