YARL, Carl!
Promise
based, easy to use, with built-in multipart/form-data
and gzip/deflate
handling support - yet another request library (yarl).
Features
Promise
based (i.e.async/await
ready)- Follows redirects
multipart/form-data
built-in supportjson
parsegzip/deflate
handlingdownload
method- etc.
Install
$ npm install yarl --save
Usage
const get download = ; ;
const post = ;const createReadStream = ;const get = ; ;
API
yarl(url, [options])
By default it's a GET
request, but you can change it in options
.
If http://
will be missed in url
, it will be automatically added.
Params:
- url (String|Object) - The URL to request or a
http.request
options object. - [options] (Object) - Any of the
http.request
options options and:- query (String|Object) - Correct
urlencoded
string or queryObject
.Object
will be stringified withquerystring.stringify
. This will override the query string inurl
. - body (String|Object|Array|Buffer) - Body that will be sent with a
POST
,PUT
,PATCH
,DELETE
request. Ifcontent-length
ortransfer-encoding
is not set in options.headers,transfer-encoding
will be set aschunked
. - multipart (Boolean) - If
true
, body object will be sent asmultipart/form-data
. - form (Boolean) - If
true
, body object will be sent asapplication/x-www-form-urlencoded
. - json (Boolean) - If
true
, body object will be sent asapplication/json
. Parse response body withJSON.parse
and set accept header toapplication/json
. If used in conjunction with theform
option, thebody
will the stringified as querystring and the response parsed as JSON. - forceRedirect (Boolean) - If
true
, will follow redirects for all methods, otherwise forGET
andHEAD
only. - redirectCount (Number) - Number of allowed redirects. By default 10.
- includeHeaders (Boolean) - If
true
,headers
property will be added to response object, otherwise onlybody
will. - buffer (Boolean) - If
true
, the body is returned as aBuffer
. - download (String|WritableStream) - Response body will be written to specified
WritableStream
or newWritableStream
will be created with specified path. - gzip (Boolean) - Unzip response body with
gzip
. Useful when server doesn't specifyContent-Encoding
header. - deflate (Boolean) - Unzip response body with
deflate
. Useful when server doesn't specifyContent-Encoding
header.
- query (String|Object) - Correct
yarl.get(url, [options])
Simmilar to yarl(url, { method: 'GET' })
.
yarl.head(url, [options])
Simmilar to yarl(url, { method: 'HEAD', includeHeaders: true })
.
yarl.post(url, [options])
Simmilar to yarl(url, { method: 'POST' })
.
yarl.put(url, [options])
Simmilar to yarl(url, { method: 'PUT' })
.
yarl.patch(url, [options])
Simmilar to yarl(url, { method: 'PATCH' })
.
yarl.delete(url, [options])
Simmilar to yarl(url, { method: 'DELETE' })
.
yarl.download(url, path)
Simmilar to yarl(url, { method: 'GET', download: path })
.
XML
You can use the xml-parser
module to parse XML data:
const yarl = ;const parse = ; { return Object;} ; // or ;
Proxies
You can use the tunnel
module with the agent
option to work with proxies:
const yarl = ;const tunnel = ; ;
Cookies
You can use the cookie
module to include cookies in a request:
const yarl = ;const cookie = ; ;
OAuth
You can use the oauth-1.0a
module to create a signed OAuth request:
const yarl = ;const crypto = ;const OAuth = ; const oauth = ; const token = key: processenvACCESS_TOKEN secret: processenvACCESS_TOKEN_SECRET; const url = 'https://api.twitter.com/1.1/statuses/home_timeline.json'; ;
Other examples
const yarl = ; async { const body: photo = await ; await }
const createReadStream = ;const get = ;const post = ; const options = body: photo: value: options: filename: 'photo.jpg' field: 1 2 '3' 4 null multipart: true json: true; ;
License
The MIT License (MIT)
Copyright (c) 2015-2017 Alexey Bystrov