From 7423f2ae3a71a45ab2be60bd8d4a3ff156240000 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sat, 8 Sep 2018 19:38:20 +0200 Subject: [PATCH] post lol --- src/clients/tg.mjs | 2 +- src/inc/fetch.mjs | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/clients/tg.mjs b/src/clients/tg.mjs index 169af31..efb1756 100644 --- a/src/clients/tg.mjs +++ b/src/clients/tg.mjs @@ -77,7 +77,7 @@ export class tg extends EventEmitter { if(msg.length === 0 || msg.length > 2048) return false; const opts = { - method: 'POST', + method: "POST", body: { chat_id: chatid, text: msg, diff --git a/src/inc/fetch.mjs b/src/inc/fetch.mjs index ea92d48..43da4de 100644 --- a/src/inc/fetch.mjs +++ b/src/inc/fetch.mjs @@ -3,11 +3,21 @@ import https from "https"; import url from "url"; export default (a, options = {}, link = url.parse(a)) => new Promise((resolve, reject) => { - (link.protocol === "https:"?https:http).get({...{ + options = {...{ hostname: link.hostname, path: link.path, method: "GET" - }, ...options}, (res, data = "") => res + }, ...options}; + let body = ""; + if(options.method === "POST") { + body = JSON.stringify(options.body); + delete options.body; + options.headers = { + "Content-Type": "application/x-www-form-urlencoded", + "Content-Length": Buffer.byteLength(body) + } + } + const post = (link.protocol === "https:"?https:http).request(options, (res, data = "") => res .setEncoding("utf8") .on("data", chunk => data += chunk) .on("end", () => resolve({ @@ -16,4 +26,5 @@ export default (a, options = {}, link = url.parse(a)) => new Promise((resolve, r buffer: () => new Buffer.from(data) })) ).on("error", err => reject(err)); + post.end(body); });