0

looked at a few examples of how to execute a webhook with parameters but can't seem to make the connection on what I am missing. Any advice on what I am doing wrong would be appreciated.

please consider: my Powershell runbook

[CmdletBinding()]
Param([object]$WebhookData) #this parameter name needs to be called 
WebHookData otherwise the webhook does not work as expected.

$VerbosePreference = 'continue'
Write-Output "hello"
    "in the inline"
    if($WebhookData -ne $null) 
        {
            "using webhookdata"
            $WebhookName =  $WebhookData.WebhookName
            $WebhookBody =  $WebhookData.RequestBody
            $webhookBodyObject = $WebhookBody | ConvertFrom-JSON


line 15            'The parameter created was ' $webhookBodyObject.strYear

My httpclient post request looks like this (warning..its vb)

dim WebHookData as new StringContent("{'strYear'='2018'}",Encoding.UTF8,"application/json")

        Dim resp as Task(Of HttpResponseMessage)
        resp = _client.PostAsync(webhook,WebHookData)
        status = resp.Result.Content.ReadAsStringAsync().Result
        if(status.Contains("JobId"))
            status = "Scheduled!"
        End If

My Webhook data is being posted to my webhook as this.

{"WebhookName":"myimportjob","RequestBody":"{'strYear'='2018'}","RequestHeader":{"Connection":"Keep-Alive","Expect":"100-continue","Host":"xxx.azure-automation.net","x-ms-request-id":"xxx"}}

I am getting this error

At line:15 char:42 + 'The parameter created was ' $webhookBodyObject.strYear + ~~~~~~~~~~~~~~~~~~ Unexpected token '$webhookBodyObject' in expression or statement.

macm
  • 544
  • 7
  • 21
  • I have also tried changing my json to dim WebHookData as new StringContent("{""strYear"":""2018""}",Encoding.UTF8,"application/json") which result in the webhookname = {"WebhookName":"RunImportOnDev","RequestBody":"{\"strYear\":\"2018\"}","Request... – macm May 03 '18 at 23:17
  • So if you comment out line 15 does your runbook execute without errors? It seems like a powershell syntax error to me. The runbook code you posted is incomplete so it's not possible to see if it's missing something. – Nick.Mc May 04 '18 at 01:27
  • Hi. It will run but I need the parameter further down in my actual code. I didn't bother pasting the other code as I didn't see it as valuable since the error seems to be that it can't get that param. – macm May 04 '18 at 01:39
  • If you commented it out and it worked then I'm guessing it's just a Powershell syntax error. Does the error happen when you save the powershell or when you call it? – Nick.Mc May 04 '18 at 02:39

1 Answers1

0

I discovered the devil is in the details. Firstly I had

$webhookBodyObject = $WebhookBody | ConvertFrom-JSON

which is not the same as

$webhookBodyObject = $WebhookBody | ConvertFrom-Json <---this is the correct syntax

The other was that the json I was sending had a single quote like this '{"key":"value"}'

For some reason, even though it passed regular powershell, the runbook didn't like it. It wants it's Json like this {"key":"value"}. I never tested complex objects so I can't speak to that.

macm
  • 544
  • 7
  • 21