How to create/update Jenkins job via REST API in Java?

97 views
Skip to first unread message

Jon Brohauge

unread,
Apr 4, 2022, 5:13:34 AM4/4/22
to jenkins...@googlegroups.com
Hi everyone,

 

I’m trying to create/update a Jenkins job via the REST API. I can successfully retrieve a crumb, so I know that my username/password combination works.

Whenever I try to POST a job, using a Basic Authentication with the “username:password” base64 encoded, I get a 403.

Whenever I try to POST a job, using a Basic Authentication with the “username:apitoken” base64 encoded, I get a 500. The apitoken has been handmade just for this test.

 

I have tried to use the example posted in Cloudbees Support (https://support.cloudbees.com/hc/en-us/articles/226835367-How-to-update-job-config-files-using-the-REST-API-and-Java-)

The project source files are located here: https://github.com/Topdanmark/jenkins-job-rest-api

 

The username, password, and server have been redacted for obvious reasons.

 

Any pointers are appreciated. A quick heads up. The project must be able to run on Java8 on Windows, due to legacy requirements.


Regards,

Jon Brohauge

Ivan Fernandez Calvo

unread,
Apr 4, 2022, 12:40:27 PM4/4/22
to Jenkins Users
if you want to manage your jobs using the API are some tools that make that work Jenkin Job Builder, also you can manage your Jobs and configuration using Jenkins configuration as Code and JobDSL plugin

Daniel Beck

unread,
Apr 11, 2022, 5:11:33 PM4/11/22
to jenkins...@googlegroups.com
On Mon, Apr 4, 2022 at 11:13 AM Jon Brohauge <jonbr...@gmail.com> wrote:
Whenever I try to POST a job, using a Basic Authentication with the “username:password” base64 encoded, I get a 403.


Either use an API token, or record the `Set-Cookie` response header and it the same session ID with every subsequent request.
  

Whenever I try to POST a job, using a Basic Authentication with the “username:apitoken” base64 encoded, I get a 500. The apitoken has been handmade just for this test.


The reason for the HTTP 500 is something the logs on Jenkins should tell you. My guess is you forgot to set `Content-Type: text/xml` as documented in `/job/whatever/api/`.
Reply all
Reply to author
Forward
0 new messages