jenkinsci / java-client-api

A Jenkins API client for Java
MIT License
901 stars 470 forks source link

invoke JenkinsServer#createJob Error #413

Open meishaoyong opened 5 years ago

meishaoyong commented 5 years ago

I use jenkins 2.164.2,and i can invoke jenkinsServer#getJobXml to get jobXml,but when i invoke jenkinsServer#createJob("my-test-job", jobXml, true) occurs the following error message:

org.apache.http.client.HttpResponseException: Internal Server Error at com.offbytwo.jenkins.client.validator.HttpResponseValidator.validateResponse(HttpResponseValidator.java:11) at com.offbytwo.jenkins.client.JenkinsHttpClient.post_xml(JenkinsHttpClient.java:375) at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:389) at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:359)

as anyone can help me?

meishaoyong commented 5 years ago

and i use jenkins-client 0.3.8, and i invoke jenkinsServer#createJob("my-test-job", jobXml) occur error too:

org.apache.http.client.HttpResponseException: Forbidden at com.offbytwo.jenkins.client.validator.HttpResponseValidator.validateResponse(HttpResponseValidator.java:11) at com.offbytwo.jenkins.client.JenkinsHttpClient.post_xml(JenkinsHttpClient.java:375) at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:389) at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:345)

meishaoyong commented 5 years ago

image

karkusviktor commented 5 years ago

Try with crumbFlag true. See: https://wiki.jenkins.io/display/JENKINS/Remote+access+API - CSRF Protection

diandiankou commented 5 years ago

I have the same problem, when I build a job, sometimes throw exceptions: image 企业微信截图_20190809094143 offbytwo version:0.3.8 jenkins on k8s:2.176.1

kumarshorav11 commented 4 years ago

@meishaoyong Hey, I have same issue with/without crumbFlag. I am trying to create job by calling createJob() method :-

HttpClientBuilder builder = HttpClientBuilder.create();
        JenkinsHttpClient client = new JenkinsHttpClient(uri, builder, "XXX", "XXX");
        JenkinsServer jenkins = new JenkinsServer(client);
        String sourceXML = readFile("src/main/resources/config.xml");
        System.out.println(String.format("Installed Jenkins Version >> %s", jenkins.getVersion().getLiteralVersion()));//works and gives correct result
        jenkins.createJob("test-nov1", sourceXML);

or another way -

HttpClientBuilder builder = HttpClientBuilder.create();
        JenkinsHttpClient client = new JenkinsHttpClient(uri, addAuthentication(builder, uri, userName, password));
        JenkinsServer jenkins = new JenkinsServer(client);
        String sourceXML = readFile("src/main/resources/config.xml");
        System.out.println(String.format("Installed Jenkins Version >> %s", jenkins.getVersion().getLiteralVersion()));
        jenkins.createJob(null,"test-nov1", sourceXML,true);

I am getting same error as of yours like below -

Exception in thread "main" org.apache.http.client.HttpResponseException: status code: 403, reason phrase: Forbidden
    at com.offbytwo.jenkins.client.validator.HttpResponseValidator.validateResponse(HttpResponseValidator.java:11)
    at com.offbytwo.jenkins.client.JenkinsHttpClient.post_xml(JenkinsHttpClient.java:375)
    at com.offbytwo.jenkins.JenkinsServer.createJob(JenkinsServer.java:389)
    at com.xxx.OffByTwoJenkins.main(OffByTwoJenkins.java:46)

Are you able to call createJob() method without error? If yes, then please post your code also and setting information in Jenkins. P.S: When I changed "Any user can do Any thing" in Jenkins under /configureSecurity, there is no error. What I found is that when without this setting, when we hit Jenkins, Jenkins still uses Anonymous user for log in. And due to this, this error is coming. Please post your thoughts and any solution/workaround if any you found. Thanks

kumarshorav11 commented 4 years ago

@meishaoyong I have same version as of yours. However, as of now, I updated to Jenkins version : 2.190.2.