Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkins rest api returns 400 nothing is submitted

Tags:

jenkins

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.

like image 584
BiAiB Avatar asked Aug 30 '18 15:08

BiAiB


People also ask

What is Jenkins Error 400?

This error means that an HTTPS request reaches to a HTTP (plaintext) listener.

What is Jenkins REST API?

The jenkins-rest library is an object oriented Java project that provides access to the Jenkins REST API programmatically to some remote API Jenkins provides. It is built using the jclouds toolkit and can easily be extended to support more REST endpoints.


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"
like image 125
BiAiB Avatar answered Sep 28 '22 06:09

BiAiB