![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
rqt
Advanced tools
Changelog
3.0.2
aqt
to have method documented.Readme
rqt
is a Node.js request library. It allows to send requests with or without data, parse a JSON response automatically and enables gzip
compression by default.
Options
Typeasync rqt(url: string, options?: Options): string
async jqt(url: string, options?: Options): Object
async bqt(url: string, options?: Options): Buffer
async aqt(url: string, options?: AqtOptions): AqtReturn
Session
Class
The package can be used from Node.js as multiple functions for different kinds of requests to make. The main purpose of splitting the package into multiple functions is to be able to get the correct type inference, e.g., a string for rqt
, buffer for bqt
and an object for jqt
, and make it visually perceivable what kind of data is expected from the server. A Session instance can be used to persist cookies across requests.
import rqt, { jqt, bqt, aqt, Session } from 'rqt'
Function | Meaning | Return type |
---|---|---|
rqt | String Request | Request a web page and return the result as a string. |
jqt | JSON Request | Parse the result as a JSON object. |
bqt | Binary Request | The result will be returned as a buffer. |
aqt | Advanced Request | In addition to the body, the result will contain headers and status, an alias for @rqt/aqt . |
Session | Session With Cookies | Proxies all other methods from this package, but remembers cookies. |
Options
TypeEach request function accept options to set headers and send data as the second argument after the URL.
import('http').OutgoingHttpHeaders
OutgoingHttpHeaders
Options
: Options for requests.
Name | Type | Description | Default |
---|---|---|---|
data | * | Optional data to send to the server with the request. | - |
type | 'form'|'json' | How to send data: json to serialise JSON data and form for url-encoded transmission with json mode by default. | 'json' |
headers | OutgoingHttpHeaders | Headers to use for the request. | - |
compress | boolean | Add the Accept-Encoding: gzip, deflate header automatically to indicate to the server that it can send a compressed response. | true |
method | string | What HTTP method to use to send data. | POST |
async rqt(
url: string,
options?: Options,
): string
Request a web page, and return the result as a string.
import rqt from 'rqt'
const Request = async (url) => {
const res = await rqt(url)
console.log(res)
}
Hello World
import idioCore from '@idio/core'
const Server = async () => {
const { url } = await idioCore({
/** @type {import('koa').Middleware} */
async hello(ctx, next) {
ctx.body = 'Hello World'
await next()
},
}, { port: 0 })
return url
}
export default Server
To send data to the server, add some options.
import rqt from 'rqt'
const Request = async (url) => {
const res = await rqt(url, {
headers: {
'User-Agent': '@rqt/rqt (Node.js)',
},
data: {
username: 'new-user',
password: 'pass123',
},
type: 'form',
method: 'PUT',
})
console.log(res)
}
You have requested with PUT:
Body: {
"username": "new-user",
"password": "pass123"
}
Headers: {
"user-agent": "@rqt/rqt (Node.js)",
"content-type": "application/x-www-form-urlencoded",
"content-length": "34",
"accept-encoding": "gzip, deflate",
"host": "localhost:5001",
"connection": "close"
}
import idioCore from '@idio/core'
const Server = async () => {
const { url } = await idioCore({
bodyparser: { use: true },
/** @type {import('koa').Middleware} */
async test(ctx, next) {
ctx.body = `You have requested with ${ctx.method}:
Body: ${JSON.stringify(ctx.request.body, null, 2)}
Headers: ${JSON.stringify(ctx.request.headers, null, 2)}
`
await next()
},
}, { port: 5001 })
return url
}
export default Server
async jqt(
url: string,
options?: Options,
): Object
Request a web page, and return the result as an object.
import { jqt } from 'rqt'
const Request = async (url) => {
const res = await jqt(url)
console.log(JSON.stringify(res, null, 2))
}
{
"Hello": "World"
}
async bqt(
url: string,
options?: Options,
): Buffer
Request a web page, and return the result as a buffer.
import { bqt } from 'rqt'
const Request = async (url) => {
const res = await bqt(url)
console.log(res)
}
<Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
async aqt(
url: string,
options?: AqtOptions,
): AqtReturn
Request a web page and return additional information about the request. This method is also available as a standalone package: @rqt/aqt
.
import('http').OutgoingHttpHeaders
OutgoingHttpHeaders
AqtOptions
: Configuration for requests.
Name | Type | Description | Default |
---|---|---|---|
data* | Object | Optional data to send to the server with the request. | - |
type | 'form'|'json' | How to send data: json to serialise JSON data and form for url-encoded transmission with json mode by default. | 'json' |
headers | OutgoingHttpHeaders | Headers to use for the request. | - |
compress | boolean | Add the Accept-Encoding: gzip, deflate header automatically to indicate to the server that it can send a compressed response. | true |
method | string | What HTTP method to use to send data. | POST |
binary | boolean | Whether to return a buffer instead of a string. | false |
justHeaders | boolean | Whether to stop the request after response headers were received, without waiting for the data. | false |
import('http').IncomingHttpHeaders
IncomingHttpHeaders
Name | Type | Description |
---|---|---|
body* | string|object|Buffer | The return from the server. In case the json content-type was set by the server, the response will be parsed into an object. If binary option was used for the request, a Buffer will be returned. Otherwise, a string response is returned. |
headers* | IncomingHttpHeaders | Incoming headers returned by the server. |
statusCode* | number | The status code returned by the server. |
statusMessage* | string | The status message set by the server. |
Session
ClassThe Session class allows to remember cookies set during all requests. It will maintain an internal state and update cookies when necessary.
constructor(
options?: SessionOptions,
): Session
Create an instance of the Session class. All headers specified in the constructor will be present for each request (unless overridden by individual request options).
SessionOptions
: Options for a session.
Name | Type | Description |
---|---|---|
host | string | The prefix to each request, such as https://rqt.biz . |
headers | OutgoingHttpHeaders | Headers to use for each request. |
The methods in the Session class are proxied to the respective methods in the API, but the cookies and session's headers will be set automatically.
import { Session } from 'rqt'
const SessionRequest = async (url) => {
// 0. Create a Session.
const session = new Session({
host: url,
headers: {
'User-Agent': 'Mozilla/5.0 Node.js rqt',
},
})
// 1. Request A JSON Page with Jqt.
const { SessionKey } = await session.jqt('/StartSession')
console.log('Session key: %s', SessionKey)
// 2. Send Form Data And Get Headers With Aqt.
const {
statusCode,
body,
headers: { location },
} = await session.aqt('/Login', {
data: {
LoginUserName: 'test',
LoginPassword: 'test',
sessionEncryptValue: SessionKey,
},
type: 'form',
})
console.log('%s Redirect: "%s"', statusCode, body)
// 3. Request A Page As A String With Rqt.
const res = await session.rqt(location)
console.log('Page: "%s"', res)
}
Session key: Example-4736gst4yd
302 Redirect: "Redirecting to <a href="/Portal">/Portal</a>."
Page: "Hello, test"
import idioCore from '@idio/core'
const Server = async () => {
const { url, app, router } = await idioCore({
/** @type {import('koa').Middleware} */
async error(ctx, next) {
try {
await next()
} catch ({ message }) {
ctx.status = 400
ctx.body = message
}
},
session: { use: true, keys: ['example'] },
bodyparser: { use: true },
}, { port: 5002 })
router.get('/StartSession', async (ctx, next) => {
ctx.session.SessionKey = 'Example-4736gst4yd'
ctx.body = {
SessionKey: ctx.session.SessionKey,
}
await next()
})
router.post('/Login', async (ctx, next) => {
const { sessionEncryptValue } = ctx.request.body
if (!sessionEncryptValue) {
throw new Error('Missing session key.')
}
if (sessionEncryptValue != ctx.session.SessionKey) {
throw new Error('Incorrect session key.')
}
ctx.session.user = ctx.request.body.LoginUserName
ctx.redirect('/Portal')
await next()
})
router.get('/Portal', async (ctx, next) => {
if (!ctx.session.user) {
throw new Error('Not authorized.')
}
ctx.body = `Hello, ${ctx.session.user}`
await next()
})
app.use(router.routes())
return url
}
export default Server
async rqt(
location: string,
options?: Options,
): String
Request a page as a string. All options are the same as accepted by the rqt
functions.
async jqt(
location: string,
options?: Options,
): String
Request a page as an object.
async bqt(
location: string,
options?: Options,
): String
Request a page as a buffer.
async aqt(
location: string,
options?: AqtOptions,
): AqtReturn
Request a page and return parsed body, headers and status.
(c) Rqt 2019
FAQs
Easy-To-Use Request Library That Supports String, JSON And Buffer Requests, Timeouts, GZip Compression And Session Maintenance.
The npm package rqt receives a total of 2 weekly downloads. As such, rqt popularity was classified as not popular.
We found that rqt demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.