request
Advanced tools
Comparing version 2.10.0 to 2.11.0
54
main.js
@@ -22,3 +22,2 @@ // Copyright 2010-2012 Mikeal Rogers | ||
, qs = require('querystring') | ||
, mimetypes = require('./mimetypes') | ||
, oauth = require('./oauth') | ||
@@ -32,2 +31,5 @@ , uuid = require('./uuid') | ||
, aws = require('./aws') | ||
, mime = require('mime') | ||
, FormData = require('form-data') | ||
; | ||
@@ -227,2 +229,14 @@ | ||
self._parserErrorHandler = function (error) { | ||
if (this.res) { | ||
if (this.res.request) { | ||
this.res.request.emit('error', error) | ||
} else { | ||
this.res.emit('error', error) | ||
} | ||
} else { | ||
this._httpMessage.emit('error', error) | ||
} | ||
} | ||
if (options.form) { | ||
@@ -328,3 +342,3 @@ self.form(options.form) | ||
if (!self.headers['content-type'] && !self.headers['Content-Type']) | ||
self.headers['content-type'] = mimetypes.lookup(src.path.slice(src.path.lastIndexOf('.')+1)) | ||
self.headers['content-type'] = mime.lookup(src.path) | ||
} else { | ||
@@ -351,2 +365,6 @@ if (src.headers) { | ||
if (self._form) { | ||
self.setHeaders(self._form.getHeaders()) | ||
self._form.pipe(self) | ||
} | ||
if (self.body) { | ||
@@ -488,5 +506,5 @@ if (Array.isArray(self.body)) { | ||
var self = this | ||
if (self._aborted) return | ||
self._started = true | ||
@@ -496,3 +514,3 @@ self.method = self.method || 'GET' | ||
if (log) log('%method %href', self) | ||
if (self.src && self.src.stat && self.src.stat.size) { | ||
@@ -504,7 +522,10 @@ self.headers['content-length'] = self.src.stat.size | ||
} | ||
self.req = self.httpModule.request(self, function (response) { | ||
if (response.connection.listeners('error').indexOf(self._parserErrorHandler) === -1) { | ||
response.connection.once('error', self._parserErrorHandler) | ||
} | ||
if (self._aborted) return | ||
if (self._paused) response.pause() | ||
self.response = response | ||
@@ -527,3 +548,3 @@ response.request = self | ||
} | ||
var addCookie = function (cookie) { | ||
@@ -630,3 +651,3 @@ if (self._jar) self._jar.add(new Cookie(cookie)) | ||
} else { | ||
response.body = body.toString() | ||
response.body = body.toString(self.encoding) | ||
} | ||
@@ -675,3 +696,5 @@ } else if (buffer.length) { | ||
}) | ||
self.on('end', function() { | ||
self.req.connection.removeListener('error', self._parserErrorHandler) | ||
}) | ||
self.emit('request', self.req) | ||
@@ -737,5 +760,10 @@ } | ||
Request.prototype.form = function (form) { | ||
this.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8' | ||
this.body = qs.stringify(form).toString('utf8') | ||
return this | ||
if (form) { | ||
this.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8' | ||
this.body = qs.stringify(form).toString('utf8') | ||
return this | ||
} | ||
// create form-data object | ||
this._form = new FormData() | ||
return this._form | ||
} | ||
@@ -742,0 +770,0 @@ Request.prototype.multipart = function (multipart) { |
{ "name" : "request" | ||
, "description" : "Simplified HTTP request client." | ||
, "tags" : ["http", "simple", "util", "utility"] | ||
, "version" : "2.10.0" | ||
, "version" : "2.11.0" | ||
, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>" | ||
@@ -14,3 +14,4 @@ , "repository" : | ||
, "main" : "./main" | ||
, "bundleDependencies": ["form-data", "mime"] | ||
, "scripts": { "test": "node tests/run.js" } | ||
} |
@@ -93,5 +93,27 @@ # Request -- Simplified HTTP request method | ||
``` | ||
You can still use intermediate proxies, the requests will still follow HTTP forwards, etc. | ||
## Forms | ||
`request` supports `application/x-www-form-urlencoded` and `multipart/form-data` form uploads. For `multipart/related` refer to the `multipart` API. | ||
Url encoded forms are simple | ||
```javascript | ||
request.post('http://service.com/upload', {form:{key:'value'}}) | ||
// or | ||
request.post('http://service.com/upload').form({key:'value'}) | ||
``` | ||
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. | ||
```javascript | ||
var r = request.post('http://service.com/upload') | ||
var form = r.form() | ||
form.append('my_field', 'my_value') | ||
form.append('my_buffer', new Buffer([1, 2, 3])) | ||
form.append('my_file', fs.createReadStream(path.join(__dirname, 'doodle.png')) | ||
form.append('remote_file', request('http://google.com/doodle.png')) | ||
``` | ||
## OAuth Signing | ||
@@ -154,3 +176,3 @@ | ||
* `body` - entity body for POST and PUT requests. Must be buffer or string. | ||
* `form` - sets `body` but to querystring representation of value and adds `Content-type: application/x-www-form-urlencoded; charset=utf-8` header. | ||
* `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. | ||
* `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json` header. Additionally, parses the response body as json. | ||
@@ -157,0 +179,0 @@ * `multipart` - (experimental) array of objects which contains their own headers and `body` attribute. Sends `multipart/related` request. See example below. |
@@ -11,2 +11,5 @@ var spawn = require('child_process').spawn | ||
, 'test-errors.js' | ||
, 'test-form.js' | ||
, 'test-follow-all-303.js' | ||
, 'test-follow-all.js' | ||
, 'test-headers.js' | ||
@@ -17,4 +20,6 @@ , 'test-httpModule.js' | ||
, 'test-oauth.js' | ||
, 'test-params.js' | ||
, 'test-pipes.js' | ||
, 'test-pool.js' | ||
, 'test-protocol-changing-redirect.js' | ||
, 'test-proxy.js' | ||
@@ -21,0 +26,0 @@ , 'test-qs.js' |
@@ -33,2 +33,7 @@ var server = require('./server') | ||
} | ||
, testGetEncoding : | ||
{ resp : server.createGetResponse(new Buffer('efa3bfcea9e29883', 'hex')) | ||
, encoding: 'hex' | ||
, expectBody: "efa3bfcea9e29883" | ||
} | ||
, testGetJSON : | ||
@@ -35,0 +40,0 @@ { resp : server.createGetResponse('{"test":true}', 'application/json') |
@@ -102,3 +102,3 @@ var server = require('./server') | ||
if (req.method === "PUT") { | ||
assert.equal(req.headers['content-type'], 'text/javascript'); | ||
assert.equal(req.headers['content-type'], 'application/javascript'); | ||
check(); | ||
@@ -105,0 +105,0 @@ } |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
SPDX disjunction
LicenseSPDX disjunction for an artifact's license information
Found 1 instance in 1 package
550184
124
8059
310
2
29
29