0

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.

nihal
  • 357
  • 1
  • 3
  • 18
  • You can also do - curl -X POST --data-urlencode "token=${TOKEN}" --data-urlencode json='{"parameter": [{"name": "org", "value": "example.com"}]}' http://localhost:80/job/remote-job-2/build – Pankaj Saini Jul 05 '20 at 12:51
  • @Pankaj Thanks for the comments. I need to do this from inside a react app. This is backend API and the parameters must be passed inside the body of a POST request(Not encoded on the URL). – nihal Jul 05 '20 at 17:49

2 Answers2

1

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();
Danielf
  • 76
  • 3
0

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.

Jeff Hutton
  • 156
  • 1
  • 2
  • 11