From 1dbc56adf4971498e860a726c795430fe451ee24 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 23 Jun 2021 19:28:33 +0100 Subject: [PATCH] Fixing some issues discovered with POSTing JSON. --- .../src/automations/steps/outgoingWebhook.js | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/server/src/automations/steps/outgoingWebhook.js b/packages/server/src/automations/steps/outgoingWebhook.js index f476d57ac5..3e56834d3f 100644 --- a/packages/server/src/automations/steps/outgoingWebhook.js +++ b/packages/server/src/automations/steps/outgoingWebhook.js @@ -77,14 +77,31 @@ module.exports.run = async function ({ inputs }) { requestBody.length !== 0 && BODY_REQUESTS.indexOf(requestMethod) !== -1 ) { - request.body = JSON.parse(requestBody) + request.body = requestBody + request.headers = { + "Content-Type": "application/json", + } } try { + // do a quick JSON parse if there is a body, to generate an error if its invalid + if (request.body) { + JSON.parse(request.body) + } const response = await fetch(url, request) + const contentType = response.headers.get("content-type") + const success = response.status === 200 + let resp + if (!success) { + resp = response.statusText + } else if (contentType && contentType.indexOf("application/json") !== -1) { + resp = await response.json() + } else { + resp = await response.text() + } return { - response: await response.json(), - success: response.status === 200, + response: resp, + success: success, } } catch (err) { /* istanbul ignore next */