diff --git a/src/utils/proxy/handlers/jsonrpc.js b/src/utils/proxy/handlers/jsonrpc.js index 5618d011c..274276122 100644 --- a/src/utils/proxy/handlers/jsonrpc.js +++ b/src/utils/proxy/handlers/jsonrpc.js @@ -20,7 +20,6 @@ export async function sendJsonRpcRequest(url, method, params, username, password const client = new JSONRPCClient(async (rpcRequest) => { const body = JSON.stringify(rpcRequest); - headers['content-length'] = Buffer.byteLength(body); const httpRequestParams = { method: "POST", headers, @@ -49,7 +48,7 @@ export async function sendJsonRpcRequest(url, method, params, username, password } catch (e) { if (e instanceof JSONRPCErrorException) { - logger.warn("Error calling JSONPRC endpoint: %s. %s", url, e.message); + logger.debug("Error calling JSONPRC endpoint: %s. %s", url, e.message); return [200, "application/json", JSON.stringify({result: null, error: {code: e.code, message: e.message}})]; } diff --git a/src/utils/proxy/http.js b/src/utils/proxy/http.js index 16b58bf70..e07f06ff8 100644 --- a/src/utils/proxy/http.js +++ b/src/utils/proxy/http.js @@ -18,10 +18,15 @@ function addCookieHandler(url, params) { }; } -export function httpsRequest(url, params) { +function handleRequest(requestor, url, params) { return new Promise((resolve, reject) => { addCookieHandler(url, params); - const request = https.request(url, params, (response) => { + if (params?.body) { + params.headers = params.headers ?? {}; + params.headers['content-length'] = Buffer.byteLength(params.body); + } + + const request = requestor.request(url, params, (response) => { const data = []; response.on("data", (chunk) => { @@ -38,7 +43,7 @@ export function httpsRequest(url, params) { reject([500, error]); }); - if (params.body) { + if (params?.body) { request.write(params.body); } @@ -46,32 +51,12 @@ export function httpsRequest(url, params) { }); } -export function httpRequest(url, params) { - return new Promise((resolve, reject) => { - addCookieHandler(url, params); - const request = http.request(url, params, (response) => { - const data = []; - - response.on("data", (chunk) => { - data.push(chunk); - }); - - response.on("end", () => { - addCookieToJar(url, response.headers); - resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]); - }); - }); - - request.on("error", (error) => { - reject([500, error]); - }); - - if (params.body) { - request.write(params.body); - } +export function httpsRequest(url, params) { + return handleRequest(https, url, params); +} - request.end(); - }); +export function httpRequest(url, params) { + return handleRequest(http, url, params); } export async function httpProxy(url, params = {}) {