1 | # ajax-request — Simplified http request
|
2 | [![NPM](https://nodei.co/npm/ajax-request.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/ajax-request/)
|
3 | ```js
|
4 | var request = require('ajax-request');
|
5 | ```
|
6 |
|
7 | ## install
|
8 | ```
|
9 | npm install ajax-request --save
|
10 | ```
|
11 |
|
12 | ### test
|
13 | ```
|
14 | mocha
|
15 | ```
|
16 |
|
17 | ## API
|
18 | ### request(options, callback)
|
19 | * {obejct|string} ``options`` required
|
20 | If the options is string, it will send get request.
|
21 | * {string} ``options.url`` required
|
22 | * {string} ``options.method`` [options.method=GET]
|
23 | The http request type
|
24 | * {obejct} ``options.data``
|
25 | if the request type is `GET`, it's appended to query string of the URL, or it's sended to remote of body.
|
26 | * {object} ``options.headers``
|
27 | An object containing request headers.
|
28 | * {string} ``options.encoding``
|
29 | Set response data encoding
|
30 | * {boolean} ``options.isBuffer`` [options.isBuffer=false]
|
31 | Parse response data to buffer
|
32 | * {boolean} ``options.json`` [options.json=false]
|
33 | Parse response data to json
|
34 | * {function} ``callback`` required
|
35 |
|
36 | ```js
|
37 | request('url', function(err, res, body) {});
|
38 |
|
39 | request({
|
40 | url: '',
|
41 | method: 'GET',
|
42 | data: {
|
43 | query1: 'value1'
|
44 | }
|
45 | }, function(err, res, body) {
|
46 |
|
47 | });
|
48 | ```
|
49 |
|
50 | ### .post
|
51 | The API same as request
|
52 | ```js
|
53 | request.post({
|
54 | url: 'url',
|
55 | data: {},
|
56 | headers: {}
|
57 | });
|
58 | ```
|
59 |
|
60 | ### .download
|
61 | * {obejct} ``options`` required
|
62 | * ``options.url`` {string} required
|
63 | * ``options.ignore`` {boolean} [options.ignore=false]
|
64 | Is the filepath ignore case.
|
65 | * ``options.rootPath`` {string} [options.rootPath='']
|
66 | The root of dest path
|
67 | * ``options.destPath`` {string|function}
|
68 | Custom the download path.
|
69 | * {function} ``callback`` required
|
70 |
|
71 | ```js
|
72 | request.download({
|
73 | url: 'path/index.png',
|
74 | rootPath: ''
|
75 | }, function(err, res, body, destpath) {});
|
76 |
|
77 | request.download({
|
78 | url: 'path/index.png',
|
79 | destPath: function(filename) {
|
80 | return filename;
|
81 | }
|
82 | }, function(err, res, body, destpath) { });
|
83 |
|
84 | ```
|
85 |
|
86 | ### .base64
|
87 | Http request image, then callback with base64 data.
|
88 | * {string} ``url`` required
|
89 | * {function} ``callback`` required
|
90 |
|
91 | ```js
|
92 | request.base64(
|
93 | 'http://res.m.ctrip.com/html5/Content/images/57.png',
|
94 | function(err, res, body) {
|
95 |
|
96 | }
|
97 | );
|
98 | ``` |
\ | No newline at end of file |