Issue
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.
Solution
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();
Answered By - Danielf
Answer Checked By - Katrina (JavaFixing Volunteer)