Browse Source

implement shortcut methods for all the HTTP verbs

http2
Sindre Sorhus 10 years ago
parent
commit
1483895159
  1. 19
      index.js
  2. 2
      package.json
  3. 10
      readme.md
  4. 6
      test.js

19
index.js

@ -123,16 +123,19 @@ function got (url, opts, cb) {
return proxy;
}
got.post = function (url, opts, cb) {
[
'get',
'post',
'put',
'patch',
'head',
'delete'
].forEach(function (el) {
got[el] = function (url, opts, cb) {
opts = opts || {};
opts.method = 'POST';
return got(url, opts, cb);
};
got.put = function (url, opts, cb) {
opts = opts || {};
opts.method = 'PUT';
opts.method = el.toUpperCase();
return got(url, opts, cb);
};
});
module.exports = got;

2
package.json

@ -13,7 +13,7 @@
"node": ">=0.10.0"
},
"scripts": {
"test": "mocha"
"test": "mocha --timeout 50000"
},
"files": [
"index.js"

10
readme.md

@ -89,13 +89,15 @@ The data you requested.
The [response object](http://nodejs.org/api/http.html#http_http_incomingmessage).
#### got.get(url, [options], [callback])
#### got.post(url, [options], [callback])
Sets options.method to POST and makes a request.
#### got.put(url, [options], [callback])
#### got.patch(url, [options], [callback])
#### got.head(url, [options], [callback])
#### got.delete(url, [options], [callback])
Sets `options.method` to the method name and makes a request.
Sets options.method to PUT and makes a request.
## Related

6
test.js

@ -1,10 +1,8 @@
/* global describe, it, before, after */
'use strict';
var assert = require('assert');
var got = require('./');
var http = require('http');
var got = require('./');
it('should do HTTP request', function (done) {
got('http://google.com', function (err, data) {
@ -104,7 +102,7 @@ it('should support timeout option', function (done) {
});
});
describe('with POST ', function () {
describe('POST', function () {
var server;
before(function (done) {

Loading…
Cancel
Save