diff options
Diffstat (limited to 'packages/web-util/src/utils/http-impl.sw.ts')
-rw-r--r-- | packages/web-util/src/utils/http-impl.sw.ts | 25 |
1 files changed, 10 insertions, 15 deletions
diff --git a/packages/web-util/src/utils/http-impl.sw.ts b/packages/web-util/src/utils/http-impl.sw.ts index 921acd63b..59bef1775 100644 --- a/packages/web-util/src/utils/http-impl.sw.ts +++ b/packages/web-util/src/utils/http-impl.sw.ts @@ -28,6 +28,8 @@ import { HttpRequestLibrary, HttpRequestOptions, HttpResponse, + encodeBody, + getDefaultHeaders, } from "@gnu-taler/taler-util/http"; /** @@ -60,20 +62,13 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary { ); } - let myBody: BodyInit | undefined = undefined; - if (requestBody != null) { - if (typeof requestBody === "string") { - myBody = requestBody; - } else if (requestBody instanceof ArrayBuffer) { - myBody = requestBody; - } else if (ArrayBuffer.isView(requestBody)) { - myBody = requestBody; - } else if (typeof requestBody === "object") { - myBody = JSON.stringify(requestBody); - } else { - throw Error("unsupported request body type"); - } - } + let myBody: ArrayBuffer | undefined = + requestMethod === "POST" ? encodeBody(requestBody) : undefined; + + const requestHeadersMap = { + ...getDefaultHeaders(requestMethod), + ...requestHeader, + }; const controller = new AbortController(); let timeoutId: any | undefined; @@ -85,7 +80,7 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary { try { const response = await fetch(requestUrl, { - headers: requestHeader, + headers: requestHeadersMap, body: myBody, method: requestMethod, signal: controller.signal, |