Skip to main content
Inspiring
March 9, 2019
Answered

JSON Webhooks

  • March 9, 2019
  • 2 replies
  • 1612 views

Hello,

This is my first venture into webhooks. I am trying to read a response from ChargeOver and insert it into a database. For now, I am just dumping it to a txt file to see what it looks like. My current code is:

<cfset HTTPRequestData = deserializeJSON(ToString(getHTTPRequestData().content))>

<cfdump var="#form#" output="C:/webhook.txt">

The webhook is posting something like this:

{

  "context_str": "package",

  "context_id": "556",

  "event": "status",

  "data": {

    "package": {

      "terms_id": 2,

      "currency_id": 1,

      "external_key": null,

      "token": "ozevrx738b02",

      "nickname": "",

      "paymethod": "inv",

      "paycycle": "yrl",

      "bill_addr1": null,

      "bill_addr2": null,

...etc

My webhook.txt file shows over and over:

struct [empty]

************************************************************************************

What am I missing?

Thanks in advance!

Gary

This topic has been closed for replies.
Correct answer ghanna1

Figured out what I was doing wrong...

<cfset requestBody = toString( getHttpRequestData().content ) />

<cfset webhook = deserializeJSON( requestBody )>

    <cfdump

        var="#webhook#"

        label="HTTP Body" output="C:/webhook.txt"

        />

2 replies

ghanna1AuthorCorrect answer
Inspiring
March 9, 2019

Figured out what I was doing wrong...

<cfset requestBody = toString( getHttpRequestData().content ) />

<cfset webhook = deserializeJSON( requestBody )>

    <cfdump

        var="#webhook#"

        label="HTTP Body" output="C:/webhook.txt"

        />

BKBK
Community Expert
Community Expert
March 9, 2019

For this to work, a form had to be submitted to the current page. But it apparently wasn't.

It might help to add code to validate the form, like this

<cfif isDefined("form.fieldnames")>

    <cfdump var="#form#" output="C:/webhook.txt">

<cfelse>

No form was submitted.

</cfif>