Socket
Socket
Sign inDemoInstall

request

Package Overview
Dependencies
Maintainers
1
Versions
126
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

request - npm Package Compare versions

Comparing version 2.27.0 to 2.28.0

.travis.yml

7

index.js

@@ -15,5 +15,6 @@ // Copyright 2010-2012 Mikeal Rogers

var Cookie = require('cookie-jar')
, CookieJar = Cookie.Jar
, cookieJar = new CookieJar
var optional = require('./lib/optional')
, Cookie = optional('tough-cookie')
, CookieJar = Cookie && Cookie.CookieJar
, cookieJar = CookieJar && new CookieJar

@@ -20,0 +21,0 @@ , copy = require('./lib/copy')

@@ -0,1 +1,3 @@

var util = require('util')
module.exports =

@@ -5,2 +7,2 @@ function debug () {

console.error('REQUEST %s', util.format.apply(util, arguments))
}
}

@@ -10,3 +10,3 @@ {

],
"version": "2.27.0",
"version": "2.28.0",
"author": "Mikeal Rogers <mikeal.rogers@gmail.com>",

@@ -28,11 +28,13 @@ "repository": {

"forever-agent": "~0.5.0",
"node-uuid": "~1.4.0",
"mime": "~1.2.9"
},
"optionalDependencies": {
"tough-cookie": "~0.9.15",
"form-data": "~0.1.0",
"tunnel-agent": "~0.3.0",
"http-signature": "~0.10.0",
"oauth-sign": "~0.3.0",
"hawk": "~1.0.0",
"aws-sign": "~0.3.0",
"oauth-sign": "~0.3.0",
"cookie-jar": "~0.3.0",
"node-uuid": "~1.4.0",
"mime": "~1.2.9",
"form-data": "~0.1.0"
"aws-sign2": "~0.5.0"
},

@@ -39,0 +41,0 @@ "scripts": {

@@ -26,3 +26,3 @@ # Request -- Simplified HTTP client

You can also stream a file to a PUT or POST request. This method will also check the file extension against a mapping of file extensions to content-types, in this case `application/json`, and use the proper content-type in the PUT request if one is not already provided in the headers.
You can also stream a file to a PUT or POST request. This method will also check the file extension against a mapping of file extensions to content-types (in this case `application/json`) and use the proper `content-type` in the PUT request (if the headers don’t already provide one).

@@ -33,3 +33,3 @@ ```javascript

Request can also pipe to itself. When doing so the content-type and content-length will be preserved in the PUT headers.
Request can also `pipe` to itself. When doing so, `content-type` and `content-length` are preserved in the PUT headers.

@@ -40,3 +40,3 @@ ```javascript

Now let's get fancy.
Now let’s get fancy.

@@ -55,3 +55,3 @@ ```javascript

You can also pipe() from a http.ServerRequest instance and to a http.ServerResponse instance. The HTTP method and headers will be sent as well as the entity-body data. Which means that, if you don't really care about security, you can do:
You can also `pipe()` from `http.ServerRequest` instances, as well as to `http.ServerResponse` instances. The HTTP method, headers, and entity-body data will be sent. Which means that, if you don't really care about security, you can do:

@@ -68,3 +68,3 @@ ```javascript

And since pipe() returns the destination stream in node 0.5.x you can do one line proxying :)
And since `pipe()` returns the destination stream in ≥ Node 0.5.x you can do one line proxying. :)

@@ -86,2 +86,3 @@ ```javascript

```
You can still use intermediate proxies, the requests will still follow HTTP forwards, etc.

@@ -93,3 +94,3 @@

Url encoded forms are simple
URL-encoded forms are simple.

@@ -102,3 +103,3 @@ ```javascript

For `multipart/form-data` we use the [form-data](https://github.com/felixge/node-form-data) library by [@felixge](https://github.com/felixge). You don't need to worry about piping the form object or setting the headers, `request` will handle that for you.
For `multipart/form-data` we use the [form-data](https://github.com/felixge/node-form-data) library by [@felixge](https://github.com/felixge). You don’t need to worry about piping the form object or setting the headers, `request` will handle that for you.

@@ -130,5 +131,5 @@ ```javascript

`sendImmediately` defaults to true, which will cause a basic authentication header to be sent. If `sendImmediately` is `false`, then `request` will retry with a proper authentication header after receiving a 401 response from the server (which must contain a `WWW-Authenticate` header indicating the required authentication method).
`sendImmediately` defaults to `true`, which causes a basic authentication header to be sent. If `sendImmediately` is `false`, then `request` will retry with a proper authentication header after receiving a `401` response from the server (which must contain a `WWW-Authenticate` header indicating the required authentication method).
Digest authentication is supported, but it only works with `sendImmediately` set to `false` (otherwise `request` will send basic authentication on the initial request, which will probably cause the request to fail).
Digest authentication is supported, but it only works with `sendImmediately` set to `false`; otherwise `request` will send basic authentication on the initial request, which will probably cause the request to fail.

@@ -183,30 +184,56 @@ ## OAuth Signing

### Custom HTTP Headers
HTTP Headers, such as `User-Agent`, can be set in the `options` object.
In the example below, we call the github API to find out the number
of stars and forks for the request repository. This requires a
custom `User-Agent` header as well as https.
```
var request = require('request');
var options = {
url: 'https://api.github.com/repos/mikeal/request',
headers: {
'User-Agent': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info.stargazers_count + " Stars");
console.log(info.forks_count + " Forks");
}
}
request(options, callback);
```
### request(options, callback)
The first argument can be either a url or an options object. The only required option is uri, all others are optional.
The first argument can be either a `url` or an `options` object. The only required option is `uri`; all others are optional.
* `uri` || `url` - fully qualified uri or a parsed url object from url.parse()
* `qs` - object containing querystring values to be appended to the uri
* `method` - http method, defaults to GET
* `headers` - http headers, defaults to {}
* `body` - entity body for PATCH, POST and PUT requests. Must be buffer or string.
* `form` - when passed an object this will set `body` but to a querystring representation of value and adds `Content-type: application/x-www-form-urlencoded; charset=utf-8` header. When passed no option a FormData instance is returned that will be piped to request.
* `uri` || `url` - fully qualified uri or a parsed url object from `url.parse()`
* `qs` - object containing querystring values to be appended to the `uri`
* `method` - http method (default: `"GET"`)
* `headers` - http headers (default: `{}`)
* `body` - entity body for PATCH, POST and PUT requests. Must be a `Buffer` or `String`.
* `form` - when passed an object, this sets `body` to a querystring representation of value, and adds `Content-type: application/x-www-form-urlencoded; charset=utf-8` header. When passed no options, a `FormData` instance is returned (and is piped to request).
* `auth` - A hash containing values `user` || `username`, `password` || `pass`, and `sendImmediately` (optional). See documentation above.
* `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json` header. Additionally, parses the response body as json.
* `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json` header. Additionally, parses the response body as JSON.
* `multipart` - (experimental) array of objects which contains their own headers and `body` attribute. Sends `multipart/related` request. See example below.
* `followRedirect` - follow HTTP 3xx responses as redirects. defaults to true.
* `followAllRedirects` - follow non-GET HTTP 3xx responses as redirects. defaults to false.
* `maxRedirects` - the maximum number of redirects to follow, defaults to 10.
* `encoding` - Encoding to be used on `setEncoding` of response data. If set to `null`, the body is returned as a Buffer.
* `pool` - A hash object containing the agents for these requests. If omitted this request will use the global pool which is set to node's default maxSockets.
* `followRedirect` - follow HTTP 3xx responses as redirects (default: `true`)
* `followAllRedirects` - follow non-GET HTTP 3xx responses as redirects (default: `false`)
* `maxRedirects` - the maximum number of redirects to follow (default: `10`)
* `encoding` - Encoding to be used on `setEncoding` of response data. If `null`, the `body` is returned as a `Buffer`.
* `pool` - A hash object containing the agents for these requests. If omitted, the request will use the global pool (which is set to node's default `maxSockets`)
* `pool.maxSockets` - Integer containing the maximum amount of sockets in the pool.
* `timeout` - Integer containing the number of milliseconds to wait for a request to respond before aborting the request
* `proxy` - An HTTP proxy to be used. Support proxy Auth with Basic Auth the same way it's supported with the `url` parameter by embedding the auth info in the uri.
* `oauth` - Options for OAuth HMAC-SHA1 signing, see documentation above.
* `proxy` - An HTTP proxy to be used. Supports proxy Auth with Basic Auth, identical to support for the `url` parameter (by embedding the auth info in the `uri`)
* `oauth` - Options for OAuth HMAC-SHA1 signing. See documentation above.
* `hawk` - Options for [Hawk signing](https://github.com/hueniverse/hawk). The `credentials` key must contain the necessary signing info, [see hawk docs for details](https://github.com/hueniverse/hawk#usage-example).
* `strictSSL` - Set to `true` to require that SSL certificates be valid. Note: to use your own certificate authority, you need to specify an agent that was created with that ca as an option.
* `jar` - Set to `true` if you want cookies to be remembered for future use, or define your custom cookie jar (see examples section)
* `aws` - object containing aws signing information, should have the properties `key` and `secret` as well as `bucket` unless you're specifying your bucket as part of the path, or you are making a request that doesn't use a bucket (i.e. GET Services)
* `strictSSL` - If `true`, requires SSL certificates be valid. **Note:** to use your own certificate authority, you need to specify an agent that was created with that CA as an option.
* `jar` - If `true`, remember cookies for future use (or define your custom cookie jar; see examples section)
* `aws` - `object` containing AWS signing information. Should have the properties `key`, `secret`. Also requires the property `bucket`, unless you’re specifying your `bucket` as part of the path, or the request doesn’t use a bucket (i.e. GET Services)
* `httpSignature` - Options for the [HTTP Signature Scheme](https://github.com/joyent/node-http-signature/blob/master/http_signing.md) using [Joyent's library](https://github.com/joyent/node-http-signature). The `keyId` and `key` properties must be specified. See the docs for other options.

@@ -216,4 +243,8 @@ * `localAddress` - Local interface to bind for network connections.

The callback argument gets 3 arguments. The first is an error when applicable (usually from the http.Client option not the http.ClientRequest object). The second is an http.ClientResponse object. The third is the response body String or Buffer.
The callback argument gets 3 arguments:
1. An `error` when applicable (usually from the `http.Client` option, not the `http.ClientRequest` object)
2. An `http.ClientResponse` object
3. The third is the `response` body (`String` or `Buffer`)
## Convenience methods

@@ -229,3 +260,3 @@

Same as request() but defaults to `method: "PUT"`.
Same as `request()`, but defaults to `method: "PUT"`.

@@ -238,3 +269,3 @@ ```javascript

Same as request() but defaults to `method: "PATCH"`.
Same as `request()`, but defaults to `method: "PATCH"`.

@@ -247,3 +278,3 @@ ```javascript

Same as request() but defaults to `method: "POST"`.
Same as `request()`, but defaults to `method: "POST"`.

@@ -264,3 +295,3 @@ ```javascript

Same as request() but defaults to `method: "DELETE"`.
Same as `request()`, but defaults to `method: "DELETE"`.

@@ -273,3 +304,3 @@ ```javascript

Alias to normal request method for uniformity.
Same as `request()` (for uniformity).

@@ -321,4 +352,5 @@ ```javascript

```
Cookies are disabled by default (else, they would be used in subsequent requests). To enable cookies set jar to true (either in defaults or in the options sent).
Cookies are disabled by default (else, they would be used in subsequent requests). To enable cookies, set `jar` to `true` (either in `defaults` or `options`).
```javascript

@@ -331,3 +363,3 @@ var request = request.defaults({jar: true})

If you to use a custom cookie jar (instead of letting request use its own global cookie jar) you do so by setting the jar default or by specifying it as an option:
To use a custom cookie jar (instead `request`’s global cookie jar), set `jar` to an instance of `request.jar()` (either in `defaults` or `options`)

@@ -334,0 +366,0 @@ ```javascript

@@ -1,4 +0,5 @@

var http = require('http')
, https = false
, tls = false
var optional = require('./lib/optional')
, http = require('http')
, https = optional('https')
, tls = optional('tls')
, url = require('url')

@@ -11,17 +12,17 @@ , util = require('util')

, oauth = require('oauth-sign')
, hawk = require('hawk')
, aws = require('aws-sign')
, httpSignature = require('http-signature')
, oauth = optional('oauth-sign')
, hawk = optional('hawk')
, aws = optional('aws-sign')
, httpSignature = optional('http-signature')
, uuid = require('node-uuid')
, mime = require('mime')
, tunnel = require('tunnel-agent')
, tunnel = optional('tunnel-agent')
, _safeStringify = require('json-stringify-safe')
, ForeverAgent = require('forever-agent')
, FormData = require('form-data')
, FormData = optional('form-data')
, Cookie = require('cookie-jar')
, CookieJar = Cookie.Jar
, cookieJar = new CookieJar
, Cookie = optional('tough-cookie')
, CookieJar = Cookie && Cookie.CookieJar
, cookieJar = CookieJar && new CookieJar

@@ -43,12 +44,3 @@ , copy = require('./lib/copy')

try {
https = require('https')
} catch (e) {}
try {
tls = require('tls')
} catch (e) {}
// Hacky fix for pre-0.4.4 https

@@ -107,2 +99,4 @@ if (https && !https.Agent) {

this.canTunnel = options.tunnel !== false && tunnel;
this.init(options)

@@ -159,3 +153,3 @@ }

// do the HTTP CONNECT dance using koichik/node-tunnel
if (http.globalAgent && self.uri.protocol === "https:") {
if (http.globalAgent && self.uri.protocol === "https:" && self.canTunnel) {
var tunnelFn = self.proxy.protocol === "http:"

@@ -186,4 +180,4 @@ ? tunnel.httpsOverHttp : tunnel.httpsOverHttps

// No option ? This can be the sign of a redirect
// As this is a case where the user cannot do anything (he didn't call request directly with this URL)
// he should be warned that it can be caused by a redirection (can save some hair)
// As this is a case where the user cannot do anything (they didn't call request directly with this URL)
// they should be warned that it can be caused by a redirection (can save some hair)
message += '. This can be caused by a crappy redirection.'

@@ -232,3 +226,2 @@ }

if (self._aborted) return
if (self.req && self.req._reusedSocket && error.code === 'ECONNRESET'

@@ -293,6 +286,10 @@ && self.agent.addRequestNoreuse) {

if (options.auth) {
if (Object.prototype.hasOwnProperty.call(options.auth, 'username')) options.auth.user = options.auth.username
if (Object.prototype.hasOwnProperty.call(options.auth, 'password')) options.auth.pass = options.auth.password
self.auth(
(options.auth.user==="") ? options.auth.user : (options.auth.user || options.auth.username ),
options.auth.pass || options.auth.password,
options.auth.sendImmediately)
options.auth.user,
options.auth.pass,
options.auth.sendImmediately
)
}

@@ -439,3 +436,3 @@

// if it's https, then we might need to tunnel now.
if (self.proxy) {
if (self.proxy && self.canTunnel) {
self.tunnel = true

@@ -503,2 +500,4 @@ var tunnelFn = self.proxy.protocol === 'http:'

if (this.ca) options.ca = this.ca
if (this.ciphers) options.ciphers = this.ciphers
if (this.secureProtocol) options.secureProtocol = this.secureProtocol
if (typeof this.rejectUnauthorized !== 'undefined') options.rejectUnauthorized = this.rejectUnauthorized

@@ -549,5 +548,5 @@

if (options.secureOptions) {
if (options.secureProtocol) {
if (poolKey) poolKey += ':'
poolKey += options.secureOptions
poolKey += options.secureProtocol
}

@@ -667,6 +666,10 @@ }

if (self._jar){
if(self._jar.add){
self._jar.add(new Cookie(cookie))
}
else cookieJar.add(new Cookie(cookie))
var targetCookieJar = self._jar.setCookie?self._jar:cookieJar;
//set the cookie if it's domain in the href's domain.
targetCookieJar.setCookie(cookie, self.uri.href, function(err){
if (err){
console.warn('set cookie failed,'+ err)
}
})
}

@@ -730,3 +733,4 @@

var ha2 = md5(self.method + ':' + self.uri.path)
var digestResponse = md5(ha1 + ':' + challenge.nonce + ':1::auth:' + ha2)
var cnonce = uuid().replace(/-/g, '')
var digestResponse = md5(ha1 + ':' + challenge.nonce + ':1:' + cnonce + ':auth:' + ha2)
var authValues = {

@@ -740,3 +744,3 @@ username: self._user,

nc: 1,
cnonce: ''
cnonce: cnonce
}

@@ -1001,3 +1005,4 @@

} else {
self.setHeader('content-type', self.headers['content-type'].split(';')[0] + '; boundary=' + self.boundary)
var headerName = self.hasHeader('content-type');
self.setHeader(headerName, self.headers[headerName].split(';')[0] + '; boundary=' + self.boundary)
}

@@ -1177,20 +1182,24 @@

this._disableCookies = true
} else if (jar && jar.get) {
// fetch cookie from the user defined cookie jar
cookies = jar.get({ url: this.uri.href })
} else {
// fetch cookie from the global cookie jar
cookies = cookieJar.get({ url: this.uri.href })
var targetCookieJar = (jar && jar.getCookieString)?jar:cookieJar;
var urihref = this.uri.href
//fetch cookie in the Specified host
targetCookieJar.getCookieString(urihref, function(err, hrefCookie){
if (err){
console.warn('get cookieString failed,' +err)
} else {
cookies = hrefCookie
}
})
}
//if need cookie and cookie is not empty
if (cookies && cookies.length) {
var cookieString = cookies.map(function (c) {
return c.name + "=" + c.value
}).join("; ")
if (this.originalCookieHeader) {
// Don't overwrite existing Cookie header
this.setHeader('cookie', this.originalCookieHeader + '; ' + cookieString)
this.setHeader('cookie', this.originalCookieHeader + '; ' + cookies)
} else {
this.setHeader('cookie', cookieString)
this.setHeader('cookie', cookies)
}

@@ -1250,2 +1259,2 @@ }

module.exports = Request
module.exports = Request

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc