Skip to content
This repository

Convenience method for PATCH #448

Merged
merged 1 commit into from about 1 year ago

2 participants

Matt Loar Mikeal Rogers
Matt Loar

This adds a convenience method for the PATCH HTTP method defined in RFC 5789.

Mikeal Rogers mikeal merged commit 42e5c3f into from February 26, 2013
Mikeal Rogers mikeal closed this February 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 23, 2013
Matt Loar Add patch convenience method. aa4a285
This page is out of date. Refresh to see the latest.
10  README.md
Source Rendered
@@ -191,7 +191,7 @@ The first argument can be either a url or an options object. The only required o
191 191
 * `qs` - object containing querystring values to be appended to the uri
192 192
 * `method` - http method, defaults to GET
193 193
 * `headers` - http headers, defaults to {}
194  
-* `body` - entity body for POST and PUT requests. Must be buffer or string.
  194
+* `body` - entity body for PATCH, POST and PUT requests. Must be buffer or string.
195 195
 * `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.
196 196
 * `auth` - A hash containing values `user` || `username`, `password` || `pass`, and `sendImmediately` (optional).  See documentation above.
197 197
 * `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json` header.  Additionally, parses the response body as json.
@@ -228,6 +228,14 @@ Same as request() but defaults to `method: "PUT"`.
228 228
 request.put(url)
229 229
 ```
230 230
 
  231
+### request.patch
  232
+
  233
+Same as request() but defaults to `method: "PATCH"`.
  234
+
  235
+```javascript
  236
+request.patch(url)
  237
+```
  238
+
231 239
 ### request.post
232 240
 
233 241
 Same as request() but defaults to `method: "POST"`.
9  main.js
@@ -597,6 +597,7 @@ Request.prototype.start = function () {
597 597
         redirectTo = response.headers.location
598 598
       } else if (self.followRedirect) {
599 599
         switch (self.method) {
  600
+          case 'PATCH':
600 601
           case 'PUT':
601 602
           case 'POST':
602 603
           case 'DELETE':
@@ -1087,7 +1088,7 @@ Request.prototype.destroy = function () {
1087 1088
   if (!this._ended) this.end()
1088 1089
 }
1089 1090
 
1090  
-// organize params for post, put, head, del
  1091
+// organize params for patch, post, put, head, del
1091 1092
 function initParams(uri, options, callback) {
1092 1093
   if ((typeof options === 'function') && !callback) callback = options
1093 1094
   if (options && typeof options === 'object') {
@@ -1143,6 +1144,7 @@ request.defaults = function (options, requester) {
1143 1144
   }
1144 1145
   var de = def(request)
1145 1146
   de.get = def(request.get)
  1147
+  de.patch = def(request.patch)
1146 1148
   de.post = def(request.post)
1147 1149
   de.put = def(request.put)
1148 1150
   de.head = def(request.head)
@@ -1175,6 +1177,11 @@ request.put = function (uri, options, callback) {
1175 1177
   params.options.method = 'PUT'
1176 1178
   return request(params.uri || null, params.options, params.callback)
1177 1179
 }
  1180
+request.patch = function (uri, options, callback) {
  1181
+  var params = initParams(uri, options, callback)
  1182
+  params.options.method = 'PATCH'
  1183
+  return request(params.uri || null, params.options, params.callback)
  1184
+}
1178 1185
 request.head = function (uri, options, callback) {
1179 1186
   var params = initParams(uri, options, callback)
1180 1187
   params.options.method = 'HEAD'
15  tests/test-defaults.js
@@ -36,6 +36,21 @@ s.listen(s.port, function () {
36 36
     counter += 1;
37 37
   });
38 38
 
  39
+  s.on('/patch', function (req, resp) {
  40
+    assert.equal(req.headers.foo, 'bar');
  41
+    assert.equal(req.headers['content-type'], null);
  42
+    assert.equal(req.method, 'PATCH')
  43
+    resp.writeHead(200, {'Content-Type': 'application/json'});
  44
+    resp.end(JSON.stringify({foo:'bar'}));
  45
+  });
  46
+
  47
+  // test post(string, object, function)
  48
+  request.defaults({headers:{foo:"bar"}}).patch(s.url + '/patch', {json: true}, function (e, r, b){
  49
+    if (e) throw e;
  50
+    assert.deepEqual('bar', b.foo);
  51
+    counter += 1;
  52
+  });
  53
+
39 54
   s.on('/post-body', function (req, resp) {
40 55
     assert.equal(req.headers.foo, 'bar');
41 56
     assert.equal(req.headers['content-type'], 'application/json');
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.