diff --git a/packages/brain/src/modules/apiClients/signer/index.ts b/packages/brain/src/modules/apiClients/signer/index.ts index 12fcbe18..20f984d3 100644 --- a/packages/brain/src/modules/apiClients/signer/index.ts +++ b/packages/brain/src/modules/apiClients/signer/index.ts @@ -191,7 +191,11 @@ export class Web3SignerApi extends StandardApi { await this.request({ method: "GET", endpoint: this.serverUpcheckEndpoint, - headers: this.originHeader + headers: { + ...this.originHeader, + "Content-Type": "text/plain; charset=utf-8", + Accept: "text/plain" // Specify the expected response content type + } }); } catch (e) { throw new SignerApiError(`Error getting (GET) server upcheck. Is Web3Signer running?: ${e.message}`); diff --git a/packages/brain/src/modules/apiClients/standard.ts b/packages/brain/src/modules/apiClients/standard.ts index 98d28d97..754db684 100644 --- a/packages/brain/src/modules/apiClients/standard.ts +++ b/packages/brain/src/modules/apiClients/standard.ts @@ -8,6 +8,8 @@ import type { Network } from "@stakingbrain/common"; export class StandardApi { private useTls = false; private requestOptions: https.RequestOptions; + private authToken?: string; // Store auth token for headers + private host?: string; // Store optional host for headers protected network: Network; constructor(apiParams: ApiParams, network: Network) { @@ -22,12 +24,10 @@ export class StandardApi { protocol: urlOptions.protocol }; - this.requestOptions.headers = { - "Content-Type": "application/json", - Accept: "application/json", - Authorization: "Bearer " + apiParams.authToken, - ...(apiParams.host && { Host: apiParams.host }) - }; + this.authToken = apiParams.authToken; + if (apiParams.host) { + this.host = apiParams.host; // Save host for later use + } if (apiParams.tlsCert) { this.requestOptions.pfx = apiParams.tlsCert; @@ -35,7 +35,9 @@ export class StandardApi { this.useTls = true; } - if (this.requestOptions.protocol?.includes("https")) this.useTls = true; + if (this.requestOptions.protocol?.includes("https")) { + this.useTls = true; + } } /** @@ -50,7 +52,7 @@ export class StandardApi { method, endpoint, body, - headers, + headers = {}, // Default to an empty object timeout }: { method: AllowedMethods; @@ -65,10 +67,32 @@ export class StandardApi { this.requestOptions.method = method; this.requestOptions.path = endpoint; + // Add default headers only if they are not provided in `headers` + const defaultHeaders: Record = { + ...(headers["Content-Type"] ? {} : { "Content-Type": "application/json" }), + ...(headers["Accept"] ? {} : { Accept: "application/json" }), + Authorization: `Bearer ${this.authToken}` + }; + + // Add optional host header if it exists + if (this.host) { + defaultHeaders.Host = this.host; + } + + // Merge headers (custom headers take precedence for defaults) + const mergedHeaders = { + ...defaultHeaders, + ...headers + }; + + this.requestOptions.headers = mergedHeaders; + if (this.useTls) { this.requestOptions.rejectUnauthorized = false; req = https.request(this.requestOptions); - } else req = http.request(this.requestOptions); + } else { + req = http.request(this.requestOptions); + } if (timeout) { req.setTimeout(timeout, () => { @@ -77,12 +101,6 @@ export class StandardApi { }); } - if (headers) { - for (const [key, value] of Object.entries(headers)) { - req.setHeader(key, value); - } - } - if (body) { req.setHeader("Content-Length", Buffer.byteLength(body)); req.write(body);