How do I pass a json formatted string as a payload to an iron.io worker via a webhook?

1.2k views Asked by At

How do I submit a json formatted string to the following script with a webhook?

Below is my script on iron.io that I want to read the payload.

import sys, json

sys.argv[8]

payload_file = None
payload = None


    for i in range(len(sys.argv)):

        if sys.argv[i] == "-payload" and (i + 1) < len(sys.argv):
            payload_file = sys.argv[i + 1]
            with open(payload_file,'r') as f:
                payload = json.loads(f.read())
                print "printing resulting payload: " + str(payload)
            break

When I queue a json formatted string within the platform, I get the desired outcome which you can see below:

json formatted string that I inputed

{u'env': u'production'}

Log shown in Iron.io

printing resulting payload: {u'env': u'production'}

I'd like to accomplish the same result via a webhook call with the following javascript.

post('https://worker-aws-us-east-1.iron.io/2/projects/PROJECTKEY/tasks/webhook?code_name=CODENAME&oauth=OAUTH', {
    payload: {
        "tasks": [{
            "code_name": "firebase_email_webhook",
            "payload": "{u'env': u'production'}"
        }]
    }
})


function post(path, params, method) {
    method = method || "post"; // Set method to post by default if not specified.

    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for (var key in params) {
        if (params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
        }
    }

    document.body.appendChild(form);
    form.submit();
}

The webhook call works fine, but the issue is that I get the following error:

Traceback (most recent call last):
  File "firebase_email_webhook.py", line 32, in <module>
    payload = json.loads(f.read())
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
1

There are 1 answers

0
thousandsofthem On BEST ANSWER

Webhook endpoint receives unmodified POST content as payload. just send json and you're done.

Code borrowed from https://stackoverflow.com/a/6587249/1758892

var arr = { City: 'Moscow', Age: 25 };
$.ajax({
    url: 'Ajax.ashx',
    type: 'POST',
    data: JSON.stringify(arr),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    async: false,
    success: function(msg) {
        alert(msg);
    }
});