1.0.3 • Published 7 years ago
@quentinadam/request v1.0.3
Request
Examples
import Request from '@quentinadam/request';
(async () => {
try {
const {statusCode, headers, body} = await (new Request({
url: 'http://www.google.com/search',
qs: {q: 'nodejs'},
})).execute();
console.log(statusCode);
console.log(headers);
console.log(body.asString());
} catch (error) {
console.error(error);
}
})();import Request from '@quentinadam/request';
(async () => {
try {
const {statusCode, headers, body} = await (new Request({
url: 'http://www.example.com/users',
method: 'POST',
form: {
name: 'Bill',
age: '38'
}
})).execute();
console.log(statusCode);
console.log(headers);
console.log(body.asJSON());
} catch (error) {
console.error(error);
}
})();Class Request
new Request({url, qs, method, headers, json, form, body, timeout, keepAlive, gzip})
url<string>urlqs<{[key: string]: string}?>(optional) object containing querystring values to be appended to the urlmethod<string? = 'GET'>(optional) HTTP method (defaults toGET)headers<{[key: string]: string | string[}?>(optional) HTTP headersjson<any?>(optional) if present, adds acontent-type: application/jsonheader, encodes the provided object in JSON format and sends it in the HTTP bodyform<{[key: string]: string | string[}?>(optional) if present, adds acontent-type: application/www-form-urlencodedheader, encodes the provided object in form format and sends it in the HTTP bodybody<Buffer?>(optional) HTTP bodytimeout<number? = 15000>(optional) timeout in milliseconds (defaults to15000)keepAlive<boolean? = true>(optional) adds aconnection: keep-aliveheader and keeps the TCP connection option (defaults totrue)gzip<boolean? = true>(optional) adds aaccept-encoding: gzipheader and decodes the gzip response (defaults totrue)
request.execute(): Promise<{statusCode, statusMessage, headers, body}>
Executes the request.
statusCode<number>statusMessage<string>headers<http.IncomingHttpHeaders>body<Body>
Class Body
body.toString(): string
Returns the body as string.
body.asString(): string
Returns the body as string.
body.asJSON(): any
Parses the body as JSON and returns the JSON object.
body.asBuffer(): Buffer
Returns the raw body as a Buffer.