A minimal HTTP client for Node.js.
- Only ~150 lines of code, 0 dependencies
- Built‑in retry and 3xx redirect following
- Automatic gzip / deflate / brotli decompression
- Custom
agentsupport (e.g.SocksProxyAgent,HttpsProxyAgent, etc.) - Pure Promise API that returns a native Node
IncomingMessagewithresponse.dataattached
$ npm i lite-request --saverequest is both a function and an object with .get/.post/... shortcuts.
import request from 'lite-request'
;(async () => {
console.log((await request.get('https://myip.hoa-js.com', { json: true })).data)
console.log((await request.get({ url: 'https://myip.hoa-js.com', json: true })).data)
console.log((await request('https://myip.hoa-js.com', { json: true })).data)
console.log((await request({ url: 'https://myip.hoa-js.com', json: true })).data)
// JSON body
const jsonRes = await request({
method: 'POST',
url: 'https://httpbin.org/post',
headers: { 'x-custom-header': 'haha' },
body: { foo: 'bar' },
json: true
})
console.log(jsonRes.data)
// x-www-form-urlencoded body
const formRes = await request.post('https://httpbin.org/post', {
headers: { 'x-custom-header': 'haha' },
form: { foo: 'bar' },
json: true
})
console.log(formRes.data)
})()Default behaviour:
maxRedirects: 10:- Automatically follows 3xx responses with a
Locationheader. - Supports relative redirects, and drops
cookie/authorizationon cross‑host redirects to avoid leaking secrets.
- Automatically follows 3xx responses with a
maxRetry: 1:- Automatically retries for:
- Certain status codes:
408 / 429 / 502 / 503 / 504 / 521 / 522 / 524. - Common network errors:
ETIMEDOUT / ECONNRESET / EADDRINUSE / ECONNREFUSED / EPIPE / ENOTFOUND / ENETUNREACH / EAI_AGAIN, etc.
- Certain status codes:
- Automatically retries for:
You can tune this behaviour via:
retryOnCode: number[]retryOnError: string[]retryDelay: number(milliseconds)maxRetry: numbermaxRedirects: number
By default, requests send:
accept-encoding: gzip, deflate, br
Responses are automatically decompressed based on content-encoding:
br→zlib.createBrotliDecompress()gzip/deflate→zlib.createUnzip()- anything else → read the raw body as‑is
The decompressed body is collected and exposed as response.data.
You can plug in any http.request‑compatible agent, for example a SocksProxyAgent:
import request from 'lite-request'
import { SocksProxyAgent } from 'socks-proxy-agent'
const agent = new SocksProxyAgent('socks://admin:[email protected]:5000')
;(async () => {
console.log((await request.get('https://myip.hoa-js.com', { json: true, agent })).data)
console.log((await request.get({ url: 'https://myip.hoa-js.com', json: true, agent })).data)
console.log((await request('https://myip.hoa-js.com', { json: true, agent })).data)
console.log((await request({ url: 'https://myip.hoa-js.com', json: true, agent })).data)
})()- Without an explicit
agent:lite-requestchooses an internal keep‑alive agent based on the protocol (httporhttps). - With an explicit
agent: your agent is used as‑is and will not be overridden.
If you need multiple clients or shared defaults, use the named Request factory:
import request, { Request } from 'lite-request'
const api = Request({
headers: { 'user-agent': 'my-client' },
maxRetry: 3
})
const res = await api.get('https://example.com', { json: true })
console.log(res.data)The default export request is equivalent to Request() called once with no arguments.
$ npm testMIT