Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
fix: send JSON headers to avoid malformed responses (#2624)
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian committed Aug 7, 2020
1 parent ff25c0d commit 971cf35
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/app/services/HttpClient.ts
Expand Up @@ -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<string, string> = {
Accept: "application/json",
"Content-Type": "application/json",
};

protected async send(
method: string,
url: string,
Expand All @@ -12,20 +17,16 @@ export class HttpClient extends Http.Request {
): Promise<Contracts.HttpResponse> {
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({
Expand Down

0 comments on commit 971cf35

Please sign in to comment.