如何使用webhook将json格式的字符串提交到以下脚本?
下面是我在iron.io上的脚本,我想阅读有效载荷。
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
当我在平台中排队一个json格式的字符串时,我得到了你想要的结果,你可以在下面看到:
json格式化了我输入的字符串
{u'env': u'production'}
Iron.io中显示的日志
printing resulting payload: {u'env': u'production'}
我想通过使用以下javascript的webhook调用来完成相同的结果。
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();
}
webhook调用工作正常,但问题是我收到以下错误:
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
答案 0 :(得分:2)
Webhook端点接收未修改的POST内容作为有效负载。只要发送json就可以了。
从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);
}
});