From 9a853c7e5878fbb543c326d48df9d64e8a84e0b5 Mon Sep 17 00:00:00 2001 From: Andy Shinn Date: Mon, 25 Jan 2021 17:19:01 -0600 Subject: [PATCH] send form urlencoded data if not CORS closes #69 --- src/webhook.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/webhook.ts b/src/webhook.ts index b0b49b4..e440bc2 100644 --- a/src/webhook.ts +++ b/src/webhook.ts @@ -1,4 +1,4 @@ -import { State } from "./runner"; +import { State, InputState } from "./runner"; export interface Webhook { url: string; @@ -18,6 +18,10 @@ export function renderBody(state: State): string { return JSON.stringify(state); } +export function renderUrlEncoded(state: State): string { + return Object.entries(state.input).map((k, i) => `${k[0]}=${k[1]}`).join('&') +} + export function send(state: State, webhook: Webhook): Promise { let options: RequestInit; @@ -34,10 +38,10 @@ export function send(state: State, webhook: Webhook): Promise { default: options = { method: "POST", - body: renderBody(state), + body: webhook.cors ? renderBody(state) : renderUrlEncoded(state), mode: webhook.cors ? "cors" : "no-cors", headers: { - "Content-Type": "application/json", + "Content-Type": webhook.cors ? "application/json" : "application/x-www-form-urlencoded", }, }; }