@jnode/request
v2.1.3
Published
Simple HTTP(s) requesting package for Node.js.
Maintainers
Readme
@jnode/request
Simple HTTP(S) requesting package for Node.js.
Installation
npm i @jnode/request@betaQuick start
Import
const { request, FormPart } = require('@jnode/request');Make a request
request('GET', 'https://example.com/').then((res) => {
console.log(res.text());
});Make a multipart/form-body request
request('POST', 'https://example.com/', [
new FormPart('foo', 'bar'),
new FormPart('awa', 'uwu')
]).then((res) => {
console.log(res.text());
});Reference
request.request(method, url[, body, headers, options])
method<string> A string specifying the HTTP request method. Default:'GET'.url<string> | <URL> The target URL to request, support bothhttpandhttps.body<string> | <Buffer> | <Uint8Array> | <stream.Readable> | <URLSearchParams> | <request.FormPart[]> | <Object[]> The request body. Using <URLSearchParams> will send aapplication/x-www-form-urlencodedrequest; using <request.FormPart[]> or <Object[]> will send amultipart/form-bodyrequest.headers<Object> An object containing request headers.Content-Lengthis automatically calculated for <string>, <Buffer>, <Uint8Array>, or <URLSearchParams> body. AndContent-Typewill be force replaced while sending amultipart/form-bodyrequest with <FormPart[]> or <Object[]> body.options<Object>multipart<string> Multipart type. Default:'form-body'.optionsinhttp.request().
- Returns: <request.RequestResponse>
Make a HTTP(S) request.
Class: request.FormPart
new request.FormPart(name, body[, headers, options])
name<string> Name of the part.body<string> | <Buffer> | <Uint8Array> | <stream.Readable> | <URLSearchParams> The body of the part. Using <URLSearchParams> will create aapplication/x-www-form-urlencodedpart.headers<Object> An object containing part headers. There is a specialfilenamefield, will be added toContent-Disposition.options<Object>
Class: request.RequestResponse
new request.RequestResponse(res)
res<http.IncomingMessage> The originalnode:httpresponse object.
requestResponse.res
- Type: <http.IncomingMessage>
requestResponse.statusCode
- Type: <number>
The response HTTP status code.
requestResponse.headers
- Type: <Object>
The response HTTP headers.
requestResponse._body
- Type: <Buffer>
Notice: You should avoid reading this property directly, use
requestRespond.buffer()instead.
requestResponse.buffer()
Receives the body of the response stream and return as a buffer.
requestResponse.text([encoding])
encoding<string> The character encoding to use. Default:'utf8'.- Returns: <Promise> Fulfills with a string.
Receives the body of the response stream and return as a string.
requestResponse.json([encoding])
encoding<string> The character encoding to use. Default:'utf8'.- Returns: <Promise> Fulfills with any of <Object>, <Array>, <string>, <number>, <boolean>, or <null>.
Receives the body of the response stream and parse string as JSON.
requestResponse.rl()
- Returns: <readline.Interface>
Provides an interface for reading data from response one line at a time.
requestResponse.sse()
- Returns: <request.EventReceiver>
Provides an interface for reading data from response as Server-Sent Events (SSE, text/event-stream).
Class: request.EventReceiver
- Extends: <EventEmitter>
An interface for reading data from <stream.Readable> as Server-Sent Events (SSE, text/event-stream).
new request.EventReceiver(res)
res<stream.Readable> The response stream or any readable stream.
Event: 'close'
Emitted when the request has been completed.
Event: 'event'
event<Object>
eventReceiver.res
- Type: <stream.Readable>
The response stream or any readable stream.
eventReceiver.rl
- Type: <readline.Interface>
An interface for reading data from response one line at a time.
eventReceiver[Symbol.asyncIterator]()
- Returns: <AsyncIterator>
Create an AsyncIterator object that iterates through each event as an object. This method allows asynchronous iteration of InterfaceConstructor objects through for await...of loops.
for await (const event of eventReceiver) {
// Each event in the event stream will be successively available here as
// { data, event, id }
}