Filip Stefanov
Filip Stefanov

Reputation: 842

How to POST with Curl to configSubmit Jenkins page

I can do it with Python but not with Curl...

$ curl -H "Content-Type: application/json; charset=UTF-8" --data-urlencode   \
   '{ "description": "This is a test job.", "displayName": "#30: Success" }' \
   -n http://localhost/job/playground/30/configSubmit


<body><h2>HTTP ERROR 400</h2>
<p>Problem accessing /job/playground/30/configSubmit. Reason:
<pre>    Nothing is submitted</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>

Upvotes: 3

Views: 2230

Answers (3)

Menglong Li
Menglong Li

Reputation: 2255

Here's the Python3 Version using requests library to modify the Jenkins job config display name and description, which also handles the crumb issue:

import requests
from requests.auth import HTTPBasicAuth

auth = HTTPBasicAuth('username', 'password')

# Use the same session for all requests
session = requests.session()

jenkins_crumb_url = "http://localhost/crumbIssuer/api/json"
resp = session.get(jenkins_crumb_url, auth=auth)
crumb_request_field = resp.json()["crumbRequestField"]
crumb = resp.json()["crumb"]

jenkins_config_url = "http://localhost/job/playground/30/configSubmit"

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    crumb_request_field: crumb
}

# the displayName, and description both should be provided
form_data = {
    "displayName": display_name,
    "description": description
}

form_data = f"json={json.dumps(form_data)}"
session.post(jenkins_config_url, headers=headers, auth=auth, data=form_data)

Upvotes: 1

Tiago L. Alves
Tiago L. Alves

Reputation: 31

@Pete answer is correct however it suffices running:

curl -F 'json={"displayName":"name","description":"a short description"}' \ http://localhost/job/playground/30/configSubmit

A bit more explanation:

  • The -F option in curl is used to post a form so -X POST not necessary.
  • json= is needed and equivalent to setting Content-Type: application/json.
  • --data-urlencode (as used in the question) when used it overrides -F - so it can't be used here.

@ebeezer issue comes from attempting to set only "description" while it is necessary to set both "description" and "displayName" for the request to succeed.

Upvotes: 2

Pete Baughman
Pete Baughman

Reputation: 3034

I struggled with this for several hours. Ultimately, this answer to a similar question got me unstuck.

It seems like Jenkins is expecting form data that has a field called json. The curl command that ultimately worked for me was

curl -X POST -F 'json={"displayName":"name","description":"a short description"}' \
  http://localhost/job/playground/30/configSubmit

Upvotes: 3

Related Questions