HTTP client, designed mainly for scraping web sites. It is not so complicated as famous request, but it is really simple tool for geting html pages for parsing data or json responses from site internal API. Sometime "less is more".
- GET and POST requests via HTTP/HTTPS
- Supports both promise way and callback way
- Proxy support via tunnel-agent
- Non-utf8 charset decoding via iconv-lite
- Auto decompression gzip/deflate
- Easy JSON-API support
- Cookies parsing/serialization
- Useful request fields
- Informative custom errors via c-e
- No any superfluous things
(node '>=10' required)
npm install scra
scra
takes url string or options object as first parameter and callback function as optional second param. If no callback - scra
returns promise. Any way scra
produce response object, same as http.ClientRequest do, but with few extra fields.
const scra = require('scra');
// promise way
scra('http://httpbin.org/get')
.then((res) => console.log(res.body))
.catch((err) => console.error(err.message));
// callback way
scra('http://httpbin.org/get', (err, res) => {
if(err) console.error(err.message);
if(res) console.log(res.body);
})
See more examples in test.
-
url
(required) - address for request. String. If protocol is omitted in string - it will be set tohttp:
, soexample.com
meanshttp://example.com
. Ifurl
is the only field in options object then first param may be this string, soscra('example.com')
is equal toscra({url: 'example.com'})
. -
headers
- http-headers for sending with a request. Object with string fields. By default there are three predefined headers:{ 'connection': 'close', 'user-agent': 'astur/scra', 'accept': '*/*', }
You may set any headers manually. Also 'Host' header will be set by node
http
module, and some headers may be set depending on the other options (see below). Such headers have a higher priority over values inheaders
option (be careful, it is not "as usual").Manually set headers will replace defaults, but will be replaced by headers from options.
-
data
- data for POST request. Ifdata
is a string with length more then 0 - this string will be sent as a request body without any conversions (ifcontent-type
header is not set it will beapplication/x-www-form-urlencoded
). Ifdata
is an object it will be stringified to json and sent as a request body (content-type
header will beapplication/json
). -
cookies
- cookies to be sent with request. Key-value object. It will be stringified and placed tocookie
header. -
compressed
- Boolean. Iftrue
setaccept-encoding
header to'gzip, deflate'
. Defaults tofalse
. -
timeout
- Number of milliseconds. Time limit for request to be done (if not - error will be thrown). Iftimeout
set to0
it means no time limit. Defaults to5000
. -
proxy
- address of proxy server. It may be both,http
orhttps
, and if protocol is omitted it will be'http'
. Nowscra
supports proxy viatunnel-agent
, so you can use proxy withhttps
sites. -
reverseProxy
- object or string, describing how to change target url to reverse-proxy url. IfreverseProxy
is object, target url part is in fieldto
and reverse-proxy url part is in fieldfrom
. Actually it is just parameters forreplace
method of url string, so it is possible to use regexp in fieldto
and replacement patterns in fieldfrom
. IfreverseProxy
is string,scra
expects it is url part before path. So, string'http://reverse-proxy.my.org'
is equal to ooject like{to: /^https?:\/\/[^/]+(\/)?/i, from: 'http://reverse-proxy.my.org$1'}
rawBody
- buffer with response body (decompressed if necessary).charset
- charset part fromcontent-type
response header.body
- response body converted to string (usingiconv-lite
ifcharset
defined.) Ifcontent-type
response header isapplication/json
andbody
is valid json string then it will be parsed to object.cookies
- key-value object with cookies fromcookies
option and fromset-cookie
response header.url
- same asurl
field in options.requestHeaders
- object with headers sent with request.requestTime
- request duration (number of milliseconds).timings
- (milliseconds) detailed timings (timestamps of all request phases):start
- time when request starts. Just moment before callinghttp(s).request
.socket
- time when socket has been created (when thehttp(s)
module'ssocket
event fires).lookup
- time when DNS has been resolved (when thenet
module'slookup
event fires).connect
- time when the server acknowledges the TCP connection (when thenet
module'sconnect
event fires).secureConnect
- (https only) time when TLS handshake has been completed (when thetls
module'ssecureConnect
event fires).responce
- time when server delivers first byte of response (when thehttp(s)
module'sresponce
event fires).end
- time when all responce data has been received (when thehttp(s)
module'send
event fires).
timingPhases
- (milliseconds) relative durations of each request phase:wait
- time spent waiting for socket (timings.socket - timings.start
).dns
- time spent performing the DNS lookup (timings.lookup - timings.socket
).tcp
- time it took to establish TCP connection between a source host and destination host (timings.connect - timings.lookup
).tls
- time spent completing a TLS handshake (timings.secureConnect - timings.connect
).responce
- time spent waiting for the initial response (timings.responce - (timings.secureConnect || timings.connect)
).read
- time spent receiving the response data (timings.end - timings.responce
).total
- time spent performing all phases of request (timings.end - timings.start
).
bytes
- just how manybytes.sent
andbytes.received
by this request.options
- rawscra
options parameter as string or object.
scra
provides two custom error classes:
const scra = require('scra');
const {TimeoutError, NetworkError} = scra;
These errors contain several useful additional properties:
url
- url used in current request.errorTime
- timestamp of the moment when error was thrown.timings
- same as in response field (but maybe some timings will be missing because the error occurred before corresponding phases).timingPhases
- same as in response field (but maybe some timingPhases will be missing because the error occurred before corresponding phases).timeout
- (only inTimeoutError
) value oftimeout
option.cause
- (only inNetworkError
) error object, thrown by corehttp
module.
MIT