From 971cf3561c2f6e4e7d5831ce06207038f2814251 Mon Sep 17 00:00:00 2001 From: Brian Faust Date: Fri, 7 Aug 2020 05:24:47 +0300 Subject: [PATCH] fix: send JSON headers to avoid malformed responses (#2624) --- src/app/services/HttpClient.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/app/services/HttpClient.ts b/src/app/services/HttpClient.ts index 0e4baee6d0..0b0c6eba9c 100644 --- a/src/app/services/HttpClient.ts +++ b/src/app/services/HttpClient.ts @@ -2,6 +2,11 @@ import { Contracts, Http } from "@arkecosystem/platform-sdk"; import fetch from "isomorphic-fetch"; export class HttpClient extends Http.Request { + private readonly headers: Record = { + Accept: "application/json", + "Content-Type": "application/json", + }; + protected async send( method: string, url: string, @@ -12,20 +17,16 @@ export class HttpClient extends Http.Request { ): Promise { let response; - if (data?.query) { - const hasQueryKeys: boolean = Object.keys(data.query).length > 0; - - if (hasQueryKeys) { - url = `${url}?${new URLSearchParams(data.query as any)}`; - } + if (data?.query && Object.keys(data?.query).length > 0) { + url = `${url}?${new URLSearchParams(data.query as any)}`; } if (method === "GET") { - response = await fetch(url); + response = await fetch(url, { headers: this.headers }); } if (method === "POST") { - response = await fetch(url, { method: "POST", body: JSON.stringify(data?.data) }); + response = await fetch(url, { method: "POST", body: JSON.stringify(data?.data), headers: this.headers }); } return new Http.Response({