Rundeck: Run Job Webhook Plugin Example - Unable to parse posted JSON data

Created on 17 Nov 2019  路  4Comments  路  Source: rundeck/rundeck

Describe the bug

This may be my misunderstanding, rather than a bug - apologies if it is!

I've created a toy Run Job Webhook as per:
https://docs.rundeck.com/docs/manual/webhooks/run-job.html#usage

The underlying job just echos out "Foo" - it takes no parameters, all other options are defaults in the webhook.

The job is tested as working when called manually.

The run-job help page isn't clear, but I'm assuming I must login via the Rundeck API, and then use the returned session information to make a POST call to the generated webhook address, eg - something like:
https://my-rundeck:4443/api/33/webhook/HyTjVclDo3vilkVMnno35mXY

I'm using Python's requests library to pass session data from one post() call to the next.

But after successfully login in the webhook call then gives this gives:
400
b'{"err":"Unable to parse posted JSON data"}'

What JSON data is it expecting me to provide?

Also it strike me that I could just use the pre-Webhook Rundeck API to call the job via UUID - in which case what is advantage of using the Run Job Webhook over the direct API call to run a job?

res = sess.post('https://my-rundeck:4443/api/33/webhook/HyTjVclDo3vilkVMnno35mXY', params={})
print(res.status_code) print(res.content)

My Rundeck detail

  • Rundeck version: 3.1.2
  • install type: deb
  • OS Name/version: ubuntu 18.04

Expected behavior

Calling the webhook runs the job, using the details already provided in the webhook definition.

Most helpful comment

Many thanks @ProTip - I suspected as much, but couldn't find the right sequence, your message filled in the blanks!

In case it helps anyone else:

  1. -k if you don't have a proper cert
  2. -X POST
  3. Make sure you include a text/plain header
  4. As per @ProTip's message always include an empty JSON payload

This gives:

$ curl -k -X POST -H "Content-Type: text/plain" --data "{}" https://my-rundeck:4443/api/33/webhook/AbcDef1234HIJK
{"msg":"ok"}

All 4 comments

Hi @falloutphil ,

I believe this may not be clear in the documentation however posting to a webhook does not require the typical API authentication; the token is embedded in the webhook URL.

Further, a valid JSON payload is required. You should be able to use an empty object: "{}".

Many thanks @ProTip - I suspected as much, but couldn't find the right sequence, your message filled in the blanks!

In case it helps anyone else:

  1. -k if you don't have a proper cert
  2. -X POST
  3. Make sure you include a text/plain header
  4. As per @ProTip's message always include an empty JSON payload

This gives:

$ curl -k -X POST -H "Content-Type: text/plain" --data "{}" https://my-rundeck:4443/api/33/webhook/AbcDef1234HIJK
{"msg":"ok"}

I struggled a bit with this as the documentation isn't great. I needed to do this in PowerShell which I managed with the below code (I'm not an expert so it can probably be done more concisely):

$WebHookUri = 'http://yourwebhookuri'
$header = @{} 
$header.add("Content-Type","text/plain") 
$body = @{} | ConvertTo-Json

$result = Invoke-RestMethod -Method Post -Uri $WebHookUri -Body $body -Headers $header
$result

msg
---
ok

I hope this helps someone.

You could try like below for passing arguments through web hooks using Python. It works fine.
url = "http://rundeckip:4440/api/35/webhook/XXYYKEKLJKLA587889AAQSHGSH#WebHookName"

content = '{"field1":"","field2":""}'
payload = json.loads(content)

payload["field1"] = "@option.Param1@"
payload["field2"] = "@option.Param2@"

headers = {
'Content-Type': 'application/json',
}
response = requests.request("POST", url, headers=headers, data = json.dumps(payload))
print(response.text.encode('utf8'))

In rundeck webhook named "WebHookName" pass below arguments after selecting job type as RunJob:
-Param1 ${data.field1} -Param2 ${data.field2}

Was this page helpful?
0 / 5 - 0 ratings