A lightweight http client adapted from phin and centra.
npm i phn
const phn = require("phn");
const res = await phn({
url: 'https://example.org'
});
-
url
- URL to request -
method
- HTTP method, default:GET
-
headers
- HTTP headers object -
query
- Object to be added tourl
as query string -
data
- Request body; json, buffer or object containing form data -
form
- object containing form data -
core
- options passed on tohttp(s).request
-
parse
- parse response body asjson
orstring
-
followRedirects
- follow redirects iftrue
-
maxRedirects
- maximum number of redirects to follow, default: infinite -
stream
- return stream asres.stream
instead ofres.body
-
compression
- handle compression, acceptbr
,gzip
anddeflate
-
timeout
- request timeout in milliseconds -
maxBuffer
- maximum response buffer size
consume http response as stream
const phn = require("phn");
const stream = await phn({
url: 'https://example.org/',
compression: true,
stream: true,
});
stream.pipe(/* ... */)
use a custom agent
const phn = require("phn");
const https = require("https");
const agent = new https.Agent({ keepAlive: true });
await phn({
url: 'https://example.org/',
core: { agent },
});
get a classic callback interface
const phn = require("phn").unpromisified;
phn('https://example.org/', (err, res) => {
if (!err) console.log(res.body);
});
set options for any subsequent request
const phn = require("phn").defaults({
method: 'POST',
parse: 'json',
timeout: 2000
});
const res = await phn('https://example.org/')
phn
is tiny and comes with no dependencies.
Package | Size |
---|---|
node-fetch | |
request | |
phin | |
axios | |
superagent | |
undici | |
got | |
needle | |
isomorphic-fetch | |
r2 | |
slim-fetch | |
phn |
phn
is a fork of phin and centra by Ethan Davis.