9

I am able to build a Jenkins job with its parameters' default values by sending a POST call to http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters

and I can override the default parameters "product", "suites" and "markers by sending to this URL: http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters?product=ALL&suites=ALL&markers=ALL

But I saw examples were the parameters can be override by sending a JSON body with new values. I am trying to do that by sending the following json bodies. Neither of them works for me.

{
        'product': 'ALL',
        'suites': 'ALL',
        'markers': 'ALL'
}

and

{
  "parameter": [
        {
            "name": "product",
            "value": "ALL"
        },
        {
            "name": "suites",
            "value": "ALL"
        },
        {
            "name": "markers",
            "value": "ALL"
        }
  ]
}

What JSON to send if I want to override the values of parameters "product", "suites" & "markers"?

RaamEE
  • 3,017
  • 4
  • 33
  • 53

2 Answers2

19

I'll leave the original question as is and elaborate here on the various API calls to trigger parameterized builds. These are the calls options that I used.

Additional documentation: https://wiki.jenkins.io/display/JENKINS/Remote+access+API

The job contains 3 parameters named: product, suites, markers

  1. Send the parameters as URL query parameters to /buildWithParameters: http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters?product=ALL&suites=ALL&markers=ALL

  2. Send the parameters as JSON data\payload to /build: http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/build

The JSON data\payload is not sent as the call's json_body (which is what confused me), but rater in the data payload as:

json:'{
       "parameter": [
                     {"name":"product", "value":"123"}, 
                     {"name":"suites", "value":"high"}, 
                     {"name":"markers", "value":"Hello"}
                    ]
      }'

And here are the CURL commands for each of the above calls:

curl -X POST -H "Jenkins-Crumb:2e11fc9...0ed4883a14a" http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/build --user "raameeil:228366f31...f655eb82058ad12d" --form json='{"parameter": [{"name":"product", "value":"123"}, {"name":"suites", "value":"high"}, {"name":"markers", "value":"Hello"}]}'

curl -X POST \ 'http://jenkins:8080/view/Orion_phase_2/job/test_remote_api_triggerring/buildWithParameters?product=234&suites=333&markers=555' \ -H 'authorization: Basic c2hsb21pb...ODRlNjU1ZWI4MjAyOGFkMTJk' \ -H 'cache-control: no-cache' \ -H 'jenkins-crumb: 0bed4c7...9031c735a' \ -H 'postman-token: 0fb2ef51-...-...-...-6430e9263c3b'

What to send to Python's requests In order to send the above calls in Python you will need to pass:

  1. headers = jenkins-crumb
  2. auth = tuple of your (user_name, user_auth_token)
  3. data = dictionary type { 'json' : json string of {"parameter":[....]} }
RaamEE
  • 3,017
  • 4
  • 33
  • 53
  • Raamee.. I'm trying to do the same, however my api call goes with blank values. Here is my call. curl -X POST -H "Jenkins-Crumb:XXXX" http://my-jenkins:8080/job/apitest/buildWithParameters \ --user "Ashwin:*****" \ --form json='{"parameter": [{"name":"param1", "value":"abc"}, {"name":"param2", "value":"def"}]}'. With command line the call works perfectly. - http://my-jenkins:8080/job/apitest/buildWithParameters?param1=value1&param2=value2 – Ashwin Dec 28 '18 at 09:21
  • I was able to rectify the issue. I had used buildWithParameters once i changed it to build, the request is going as expected. – Ashwin Jan 02 '19 at 07:38
2

curl -v POST http://user:token@host:port/job/my-job/build --data-urlencode json='{"parameter": [{"name":"xx", "value":"xxx"}]}

or use Python request:

import requests
import json
url = " http://user:token@host:port/job/my-job/build "
pyload = {"parameter": [
    {"name":"xx", "value":"xxx"},
]}
data = {'json': json.dumps(pyload)}
rep = requests.post(url, data)
ahprosim
  • 308
  • 1
  • 5