|
| 1 | +# superagent |
| 2 | + |
| 3 | +[](https://travis-ci.org/visionmedia/superagent) |
| 4 | +[](https://codecov.io/gh/visionmedia/superagent) |
| 5 | +[](https://github.com/sindresorhus/xo) |
| 6 | +[](https://github.com/prettier/prettier) |
| 7 | +[](https://lass.js.org) |
| 8 | +[](LICENSE) |
| 9 | + |
| 10 | +> Small progressive client-side HTTP request library, and Node.js module with the same API, sporting many high-level HTTP client features |
| 11 | +
|
| 12 | + |
| 13 | +## Table of Contents |
| 14 | + |
| 15 | +* [Install](#install) |
| 16 | +* [Usage](#usage) |
| 17 | + * [Node](#node) |
| 18 | + * [Browser](#browser) |
| 19 | +* [Supported Platforms](#supported-platforms) |
| 20 | + * [Required Browser Features](#required-browser-features) |
| 21 | +* [Plugins](#plugins) |
| 22 | +* [Upgrading from previous versions](#upgrading-from-previous-versions) |
| 23 | +* [Contributors](#contributors) |
| 24 | +* [License](#license) |
| 25 | + |
| 26 | + |
| 27 | +## Install |
| 28 | + |
| 29 | +[npm][]: |
| 30 | + |
| 31 | +```sh |
| 32 | +npm install superagent |
| 33 | +``` |
| 34 | + |
| 35 | +[yarn][]: |
| 36 | + |
| 37 | +```sh |
| 38 | +yarn add superagent |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +## Usage |
| 43 | + |
| 44 | +### Node |
| 45 | + |
| 46 | +```js |
| 47 | +const superagent = require('superagent'); |
| 48 | + |
| 49 | +superagent |
| 50 | + .post('/api/pet') |
| 51 | + .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body |
| 52 | + .set('X-API-Key', 'foobar') |
| 53 | + .set('accept', 'json') |
| 54 | + .end((err, res) => { |
| 55 | + // Calling the end function will send the request |
| 56 | + }); |
| 57 | +``` |
| 58 | + |
| 59 | +### Browser |
| 60 | + |
| 61 | +**The browser-ready, minified version of `superagent` is only 19 KB!** |
| 62 | + |
| 63 | +Browser-ready versions of this module are available via [jsdelivr][], [unpkg][], and also in the `node_modules/superagent/dist` folder in downloads of the `superagent` package. |
| 64 | + |
| 65 | +> Note that we also provide unminified versions with `.js` instead of `.min.js` file extensions. |
| 66 | +
|
| 67 | +#### VanillaJS |
| 68 | + |
| 69 | +This is the solution for you if you're just using `<script>` tags everywhere! |
| 70 | + |
| 71 | +```html |
| 72 | +<script src="https://cdn.jsdelivr.net/npm/superagent"></script> |
| 73 | +<!-- if you wish to use unpkg.com instead: --> |
| 74 | +<!-- <script src="https://unpkg.com/superagent"></script> --> |
| 75 | +<script type="text/javascript"> |
| 76 | + (function() { |
| 77 | + // superagent is exposed as `window.superagent` |
| 78 | + // if you wish to use "request" instead please |
| 79 | + // uncomment the following line of code: |
| 80 | + // `window.request = superagent;` |
| 81 | + superagent |
| 82 | + .post('/api/pet') |
| 83 | + .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body |
| 84 | + .set('X-API-Key', 'foobar') |
| 85 | + .set('accept', 'json') |
| 86 | + .end((err, res) => { |
| 87 | + // Calling the end function will send the request |
| 88 | + }); |
| 89 | + })(); |
| 90 | +</script> |
| 91 | +``` |
| 92 | + |
| 93 | +#### Bundler |
| 94 | + |
| 95 | +If you are using [browserify][], [webpack][], [rollup][], or another bundler, then you can follow the same usage as [Node](#node) above. |
| 96 | + |
| 97 | + |
| 98 | +## Supported Platforms |
| 99 | + |
| 100 | +* Node: v6.x+ |
| 101 | +* Browsers (see [.browserslistrc](.browserslistrc)): |
| 102 | + |
| 103 | + ```sh |
| 104 | + npx browserslist |
| 105 | + ``` |
| 106 | + |
| 107 | + ```sh |
| 108 | + and_chr 71 |
| 109 | + and_ff 64 |
| 110 | + and_qq 1.2 |
| 111 | + and_uc 11.8 |
| 112 | + android 67 |
| 113 | + android 4.4.3-4.4.4 |
| 114 | + baidu 7.12 |
| 115 | + bb 10 |
| 116 | + bb 7 |
| 117 | + chrome 73 |
| 118 | + chrome 72 |
| 119 | + chrome 71 |
| 120 | + edge 18 |
| 121 | + edge 17 |
| 122 | + firefox 66 |
| 123 | + firefox 65 |
| 124 | + ie 11 |
| 125 | + ie 10 |
| 126 | + ie 9 |
| 127 | + ie_mob 11 |
| 128 | + ie_mob 10 |
| 129 | + ios_saf 12.0-12.1 |
| 130 | + ios_saf 11.3-11.4 |
| 131 | + op_mini all |
| 132 | + op_mob 46 |
| 133 | + op_mob 12.1 |
| 134 | + opera 58 |
| 135 | + opera 57 |
| 136 | + safari 12 |
| 137 | + safari 11.1 |
| 138 | + samsung 8.2 |
| 139 | + samsung 7.2-7.4 |
| 140 | + ``` |
| 141 | + |
| 142 | +### Required Browser Features |
| 143 | + |
| 144 | +* IE9 requires a polyfill for `window.FormData` (we recommend [formdata-polyfill][]) |
| 145 | + |
| 146 | + |
| 147 | +## Plugins |
| 148 | + |
| 149 | +SuperAgent is easily extended via plugins. |
| 150 | + |
| 151 | +```js |
| 152 | +const nocache = require('superagent-no-cache'); |
| 153 | +const superagent = require('superagent'); |
| 154 | +const prefix = require('superagent-prefix')('/static'); |
| 155 | + |
| 156 | +superagent |
| 157 | + .get('/some-url') |
| 158 | + .query({ action: 'edit', city: 'London' }) // query string |
| 159 | + .use(prefix) // Prefixes *only* this request |
| 160 | + .use(nocache) // Prevents caching of *only* this request |
| 161 | + .end((err, res) => { |
| 162 | + // Do something |
| 163 | + }); |
| 164 | +``` |
| 165 | + |
| 166 | +Existing plugins: |
| 167 | + |
| 168 | +* [superagent-no-cache](https://github.com/johntron/superagent-no-cache) - prevents caching by including Cache-Control header |
| 169 | +* [superagent-prefix](https://github.com/johntron/superagent-prefix) - prefixes absolute URLs (useful in test environment) |
| 170 | +* [superagent-suffix](https://github.com/timneutkens1/superagent-suffix) - suffix URLs with a given path |
| 171 | +* [superagent-mock](https://github.com/M6Web/superagent-mock) - simulate HTTP calls by returning data fixtures based on the requested URL |
| 172 | +* [superagent-mocker](https://github.com/shuvalov-anton/superagent-mocker) — simulate REST API |
| 173 | +* [superagent-cache](https://github.com/jpodwys/superagent-cache) - A global SuperAgent patch with built-in, flexible caching |
| 174 | +* [superagent-cache-plugin](https://github.com/jpodwys/superagent-cache-plugin) - A SuperAgent plugin with built-in, flexible caching |
| 175 | +* [superagent-jsonapify](https://github.com/alex94puchades/superagent-jsonapify) - A lightweight [json-api](http://jsonapi.org/format/) client addon for superagent |
| 176 | +* [superagent-serializer](https://github.com/zzarcon/superagent-serializer) - Converts server payload into different cases |
| 177 | +* [superagent-httpbackend](https://www.npmjs.com/package/superagent-httpbackend) - stub out requests using AngularJS' $httpBackend syntax |
| 178 | +* [superagent-throttle](https://github.com/leviwheatcroft/superagent-throttle) - queues and intelligently throttles requests |
| 179 | +* [superagent-charset](https://github.com/magicdawn/superagent-charset) - add charset support for node's SuperAgent |
| 180 | +* [superagent-verbose-errors](https://github.com/jcoreio/superagent-verbose-errors) - include response body in error messages for failed requests |
| 181 | + |
| 182 | +Please prefix your plugin with `superagent-*` so that it can easily be found by others. |
| 183 | + |
| 184 | +For SuperAgent extensions such as couchdb and oauth visit the [wiki](https://github.com/visionmedia/superagent/wiki). |
| 185 | + |
| 186 | + |
| 187 | +## Upgrading from previous versions |
| 188 | + |
| 189 | +Our breaking changes are mostly in rarely used functionality and from stricter error handling. |
| 190 | + |
| 191 | +* [4.x to 5.x](https://github.com/visionmedia/superagent/releases/tag/v5.0.0): |
| 192 | + * Ensure you're running Node 8.8.1 or later. |
| 193 | + * We've implemented the build setup of [Lass](https://lass.js.org) to simplify our stack and linting |
| 194 | + * Browserified build size has been reduced from 48KB to 19KB (via `tinyify` and the latest version of Babel using `@babel/preset-env` and `.browserslistrc`) |
| 195 | + * Linting support has been added using `caniuse-lite` and `eslint-plugin-compat` |
| 196 | + * We can now target what versions of Node we wish to support more easily using `.babelrc` |
| 197 | +* [3.x to 4.x](https://github.com/visionmedia/superagent/releases/tag/v4.0.0-alpha.1): |
| 198 | + * Ensure you're running Node 6 or later. We've dropped support for Node 4. |
| 199 | + * We've started using ES6 and for compatibility with Internet Explorer you may need to use Babel. |
| 200 | + * We suggest migrating from `.end()` callbacks to `.then()` or `await`. |
| 201 | +* [2.x to 3.x](https://github.com/visionmedia/superagent/releases/tag/v3.0.0): |
| 202 | + * Ensure you're running Node 4 or later. We've dropped support for Node 0.x. |
| 203 | + * Test code that calls `.send()` multiple times. Invalid calls to `.send()` will now throw instead of sending garbage. |
| 204 | +* [1.x to 2.x](https://github.com/visionmedia/superagent/releases/tag/v2.0.0): |
| 205 | + * If you use `.parse()` in the _browser_ version, rename it to `.serialize()`. |
| 206 | + * If you rely on `undefined` in query-string values being sent literally as the text "undefined", switch to checking for missing value instead. `?key=undefined` is now `?key` (without a value). |
| 207 | + * If you use `.then()` in Internet Explorer, ensure that you have a polyfill that adds a global `Promise` object. |
| 208 | +* 0.x to 1.x: |
| 209 | + * Instead of 1-argument callback `.end(function(res){})` use `.then(res => {})`. |
| 210 | + |
| 211 | + |
| 212 | +## Contributors |
| 213 | + |
| 214 | +| Name | |
| 215 | +| ------------------- | |
| 216 | +| **Kornel Lesiński** | |
| 217 | +| **Peter Lyons** | |
| 218 | +| **Hunter Loftis** | |
| 219 | +| **Nick Baugh** | |
| 220 | + |
| 221 | + |
| 222 | +## License |
| 223 | + |
| 224 | +[MIT](LICENSE) © TJ Holowaychuk |
| 225 | + |
| 226 | + |
| 227 | +## |
| 228 | + |
| 229 | +[npm]: https://www.npmjs.com/ |
| 230 | + |
| 231 | +[yarn]: https://yarnpkg.com/ |
| 232 | + |
| 233 | +[formdata-polyfill]: https://www.npmjs.com/package/formdata-polyfill |
| 234 | + |
| 235 | +[jsdelivr]: https://www.jsdelivr.com/ |
| 236 | + |
| 237 | +[unpkg]: https://unpkg.com/ |
| 238 | + |
| 239 | +[browserify]: https://github.com/browserify/browserify |
| 240 | + |
| 241 | +[webpack]: https://github.com/webpack/webpack |
| 242 | + |
| 243 | +[rollup]: https://github.com/rollup/rollup |
0 commit comments