Http Request in Node.js
Making HTTP requests using the native request library.
Making Requests
In the browser, making HTTP requests is fairly easy with the fetch api. Perhaps we'll have an analogous api in a future version of node, but right now we have the http and https modules and the request function.
The Code
I'm going to pull in my safe-types library to enable type safety for both our success and error outcomes.
import { Agent, request, RequestOptions, IncomingHttpHeaders } from "http";
import { Task } from "safe-types";
const agent = new Agent({
/**
* Keep sockets around in a pool to be used by other requests in the future. Default = false
*/
keepAlive: false,
/**
* Maximum number of sockets to allow per host. Default for Node 0.10 is 5, default for Node 0.12 is Infinity
*/
maxSockets: 5,
/**
* Socket timeout in milliseconds. This will set the timeout after the socket is connected.
*/
timeout: 15 * 1000,
});
export interface ResponseWrapper<T> {
statusCode: number | undefined;
headers: IncomingHttpHeaders;
data: T;
}
export function requestJSON<T = any>(
options: Omit<RequestOptions, "agent">,
data: any
): Task<ResponseWrapper<T>, Error> {
let reqOptions: RequestOptions = {
...options,
agent,
headers: {
...options.headers,
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(data),
},
};
return new Task(({ Ok, Err }) => {
let req = request(reqOptions, res => {
res.setEncoding("utf8");
let buf = "";
res.on("data", chunk => {
if (typeof chunk !== "string") {
console.error(chunk);
throw new Error(`chunk is not a string`);
}
buf += chunk;
});
res.on("end", () => {
let { statusCode = 0, headers } = res;
let data: any;
try {
data = JSON.parse(buf);
} catch (error) {
if (error instanceof Error) {
return Err(error);
}
console.error(error);
return Err(new Error(`Failed to parse the response as JSON.`));
}
let wrapped = {
statusCode,
headers,
data,
};
if (statusCode < 200 || statusCode >= 300) {
return Err(new Error(JSON.stringify(wrapped, null, 2)));
}
Ok(wrapped);
});
});
req.on("error", Err);
req.write(data);
req.end();
});
}
Last updated