
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@request/interface
Advanced tools
A module conforming to this specification is:
module.exports = (options) => {
// do something with options
// and make the actual HTTP request
}
Given the above module definition, a client application can use it like this:
var request = require('my-http-client')
// make request
request({
// any common option defined in this specification
})
var http = require('http')
module.exports = (options) => {
// do something with the common interface options
var resultOptions = {}
// implement various HTTP features
return http.request(resultOptions)
}
var request = require('request')
module.exports = (options) => {
// do something with the common interface options
var resultOptions = {}
// implement various HTTP features
return request(resultOptions)
}
// use the native fetch API in the browser
module.exports = (options) => {
// do something with the common interface options
var resultOptions = {}
// implement various HTTP features
return fetch(new Request(url, resultOptions))
}
Either way the client application should be able to make requests in a consistent way:
var request = require('my-http-client')
// make request
request({
// any common option defined in this specification
})
A module conforming to this specification while having optional dependencies may look like this:
module.exports = (deps) => (options) => {
var resultOptions = {}
if (options.oauth) {
resultOptions.oauth = deps.oauth(options.oauth)
}
return request(resultOptions)
}
Given the above module definition, a client application can use it like this:
var request = require('my-http-client')({
oauth: require('my-oauth-implementation')
})
// make request
request({
// any common option defined in this specification
})
A module conforming to this specification while having hardcoded dependencies may look like this:
module.exports = require('my-http-client')({
oauth: require('my-oauth-implementation')
})
Given the above module definition, a client application can use it like this:
var request = require('my-http-client')
// make request
request({
// any common option defined in this specification
})
A module using the common @request/api may look like this:
var request = require('my-http-client')
var api = require('@request/api')
module.exports = api({
type: 'basic',
request: request
})
Given the above module definition, a client application can use it like this:
var request = require('my-http-client')
// make request
request('url', {options}, (err, res, body) => {})
// or
request.[HTTP_VERB]('url', {options}, (err, res, bdoy) => {})
// + any combination of the above arguments
A module using the common @request/api may look like this:
var request = require('my-http-client')
var api = require('@request/api')
module.exports = api({
type: 'chain',
config: {
method: {
get: [],
// ...
},
option: {
qs: [],
// ...
},
custom: {
submit: [],
// ...
}
},
define: {
submit: function (callback) {
if (callback) {
this._options.callback = callback
}
return request(this._options)
}
}
})
Given the above module definition, a client application can use it like this:
var request = require('my-http-client')
// make request
request
.get('url')
.qs({a: 1})
.submit((err, res, body) => {})
A module utilizing Promises may look like this:
module.exports = (deps) => (options) => {
var request = deps.request
if (deps.promise) {
var Promise = deps.promise
var promise = new Promise((resolve, reject) => {
options.callback = (err, res, body) => {
if (err) {
reject(err)
}
else {
resolve([res, body])
}
}
})
request(options)
return promise
}
else {
return request(options)
}
}
Given the above module definition, a client application can use it like this:
var request = require('my-http-client')({
request: require('request'),
promise: Promise
})
// or
var request = require('my-http-client')({
request: require('request'),
promise: require('bluebird')
})
// make request
request({options})
.catch((err) => {})
.then((result) => {})
Promises can be combined with the @request/api.
option | type |
---|---|
method | String |
URL | |
url/uri | String , Object |
qs | Object , String |
Body | |
form | Object , String |
json | Object , String |
body | Stream , Buffer , Array , String |
multipart | Object , Array |
Authentication | |
auth | Object |
basic, oauth, hawk, httpSignature, aws | |
Modifiers | |
gzip | Boolean , String |
encoding | Boolean , String |
stringify | Object |
parse | Object |
Proxy | |
proxy | String , Object |
tunnel | Boolean |
Misc | |
headers | Object |
cookie | Boolean , Object |
length | Boolean |
callback | Function |
redirect | Boolean , Object |
timeout | Number |
har | Object |
end | Boolean |
String
String
| Object
String
url.Url
Object
| String
Object
String
Object
| String
Object
String
pass URL encoded string if you want it to be RFC3986 encoded prior sendingObject
| String
Object
String
String
| Buffer
| Array
| Stream
Stream
Buffer
String
Array
Object
| Array
Object
for multipart/form-data
Array
for any other multipart/[TYPE]
, defaults to multipart/related
Each item's body can be either: Stream
, Request
, Buffer
or String
.
_multipart
data
- the above
Additionally you can set preambleCRLF
and/or postambleCRLF
to true
.Object
basic
{user: '', pass: '', sendImmediately: false, digest: true}
Authorization: Basic ...
header.sendImmediately
option default to true
if omitted.sendImmediately: false
options requires the redirect option to be enabled.bearer
{token: '', sendImmediately: false}
Authorization: Bearer ...
header can be set if using the bearer
option.sendImmediately
option from above applies here.oauth
hawk
httpSignature
aws
Boolean
| String
true
content-encoding
response header.'gzip'
| 'deflate'
Boolean
| String
true
utf8
.'ISO-8859-1'
| 'win1251'
| ...
'binary'
encoding
to 'binary'
when expecting binary response.Object
{json: true}
accept: application/json
header for the requestJSON
or JSONP
response bodies (only if the server responds with the approprite headers){json: function () {}}
JSON.parse
method{qs: {sep:';', eq:':'}}
{querystring: {sep:';', eq:':', options: {}}}
use the querystring module instead
Object
{qs: {sep:';', eq:':'}}
{querystring: {sep:';', eq:':', options: {}}}
use the querystring module instead
String
| Object
{
proxy: 'http://localhost:6767'
//
proxy: url.parse('http://localhost:6767')
//
proxy: {
url: 'http://localhost:6767',
headers: {
allow: ['header-name'],
exclusive: ['header-name']
}
}
}
Boolean
true
Object
Boolean
| Object
true
new require('tough-cookie).CookieJar(store, options)
Boolean
true
defaults to false
if omittedFunction
function (err, res, body) {}
buffers the response body
utf8
.
Set the encoding
property to binary
if you expect binary data, or any other specific encoding.Boolean
| Object
true
GET
, HEAD
, OPTIONS
and TRACE
requestsObject
referer
header on redirectfunction (res)
user defined function to check if the redirect should be allowedNumber
Object
Boolean
true
nextTick
FAQs
Common Interface for HTTP Clients
The npm package @request/interface receives a total of 53,316 weekly downloads. As such, @request/interface popularity was classified as popular.
We found that @request/interface 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.