From 094c272294b36effc16c08b9b4e7f193d5b1b631 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 20 Apr 2021 17:39:47 +1000 Subject: [PATCH] allow postrequest to send a binary body; fix WithGraphData As far as I can tell, the existing code was transforming the encoded protobuf data into UTF8, and we're just lucky it wasn't causing problems with the small message we were sending. --- ts/graphs/WithGraphData.svelte | 4 +--- ts/lib/postrequest.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ts/graphs/WithGraphData.svelte b/ts/graphs/WithGraphData.svelte index 674ef6d28..04a33cfd6 100644 --- a/ts/graphs/WithGraphData.svelte +++ b/ts/graphs/WithGraphData.svelte @@ -38,9 +38,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html ): Promise { await postRequest( "/_anki/setGraphPreferences", - new TextDecoder().decode( - pb.BackendProto.GraphPreferences.encode(prefs).finish() - ) + pb.BackendProto.GraphPreferences.encode(prefs).finish() ); } diff --git a/ts/lib/postrequest.ts b/ts/lib/postrequest.ts index a3075361b..d36b3cdeb 100644 --- a/ts/lib/postrequest.ts +++ b/ts/lib/postrequest.ts @@ -1,9 +1,17 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -export async function postRequest(path: string, body: string): Promise { +export async function postRequest( + path: string, + body: string | Uint8Array +): Promise { + const headers = {}; + if (body instanceof Uint8Array) { + headers["Content-type"] = "application/octet-stream"; + } const resp = await fetch(path, { method: "POST", + headers, body, }); if (!resp.ok) {