jenkins rest api returns 400 nothing is submitted

Viewed 7960

I try to launch a Jenkins build via its API using cURL:

#!/usr/bin/env bash
curl \
    -i \
    --fail \
    --show-error \
    -s \
    -X POST \
    -H 'Content-Type:application/json' \
    -H 'Accept:application/json' \
    --form json='{"parameter": [{"name":"COMPOSE_FULL_NAME", "value": "/redacted/docker-compose-prod.yml"}, {"name":"BRANCH", "value": "prod"}, {"name":"AD_USER", "value": "redacted"}, {"name":"AD_PASSWORD", "value": "redacted"}}]}' \
    -u redactedUser:redactedToken \
    -k \
    https://jenkins-dck.redacted/job/elr-156344/job/stack_deploy/build \

and this is what I get:

curl: (22) The requested URL returned error: 400 Nothing is submitted

I tried several ways of passing POST data, like using -d or --data-urlencode 'json={ but with no success so far.

Any idea what's going on ? the message doesn't say much and I can't access the logs of the jenkins backend.

1 Answers

ok, found it, you first need to disregard the docs here: https://wiki.jenkins.io/display/JENKINS/Remote+access+API. The proper method is described at https://wiki.jenkins.io/display/JENKINS/Parameterized+Build

use this API endpoint:

https://jenkins-dck.redacted/job/elr-156344/job/stack_deploy/buildWithParameters?param1=urlencode&param2=urlencoded

Don't forget to quote the url in the CURL quote, since bash will mess with & symbols.

working example:

#!/usr/bin/env bash
curl \
    -i \
    --fail \
    --show-error \
    -s \
    -X POST \
    -H 'Content-Type:application/json' \
    -H 'Accept:application/json' \
    -u redactedUser:redactedToken \
    -k \
    "https://jenkins-dck.redacted/job/elr-156344/job/stack_deploy/buildWithParameters?BRANCH=prod&AD_USER=$SERVICE_ACCOUNT"
Related