nihal
nihal

Reputation: 377

How do I pass parameters in body of the POST request to a Jenkins remotely triggered job?

I have created a Jenkins job that can be triggered through a POST API call(i.e. buildWithParameters endpoint).

If I encode the parameters in URL, it works just fine. See an example below:

http://localhost:80/job/remote-job-2/buildWithParameters?token=password123&org=example.com

However, I would like to pass the parameters in the body of this POST request as shown below:

{
"token": "password123"
"org" : "example.com"
}

Note: The closest answer I could find is this What is the format of the JSON for a Jenkins REST buildWithParameters to override the default parameters values. Unfortunately, this does not work for me when I run this on postman. It fails with a +500 server error.

Upvotes: 0

Views: 2885

Answers (2)

Jeff Hutton
Jeff Hutton

Reputation: 166

In python I was driven crazy until I saw that in order to post form data I had to pass a dictionary to "data=", not a json string. Here is an example:

r = requests.post('http://localhost:8080/job/Test/job/WithParams/buildWithParameters', auth = ('a_username', 'a_password'), data = { "delay": "0","Weight": "32", "Height": "14"})

The "delay": "0" will also build immediately, ignoring any "quiet period" set on the Jenkins job.

Upvotes: 0

Danielf
Danielf

Reputation: 86

After much testing by sending a json:

'{
  "parameter": [
                 {"name":"token", "value":"password123"}, 
                 {"name":"org", "value":"example.com"}
               ]
}'

or

'{
   {"name":"token", "value":"password123"},
   {"name":"org", "value":"example.com"}
}'

And many other combinations have only worked for me by sending the parameters as standard html form fields.

For example in java/spring/restTemplate:

MultiValueMap<String, String> jobParams= new LinkedMultiValueMap<>();
jobParams.add("token", "password123");
jobParams.add("org", "example.com");
return restTemplate.exchange(jarvisUri, HttpMethod.POST, new HttpEntity<>(jobParams, headers), String.class).getBody();

Upvotes: 1

Related Questions