Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features. Maintained for Forward Email and Lad.
const superagent = require('superagent');// callbacksuperagent .post('/api/pet') .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body .set('X-API-Key', 'foobar') .set('accept', 'json') .end((err, res) => { // Calling the end function will send the request });// promise with then/catchsuperagent.post('/api/pet').then(console.log).catch(console.error);// promise with async/await
Browser
The browser-ready, minified version of superagent is only 50 KB (minified and gzipped).
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.
Note that we also provide unminified versions with .js instead of .min.js file extensions.
VanillaJS
This is the solution for you if you're just using <script> tags everywhere!
<script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=WeakRef,BigInt"></script><script src="https://cdn.jsdelivr.net/npm/superagent"></script><!-- if you wish to use unpkg.com instead: --><!-- <script src="https://unpkg.com/superagent"></script> --><script type="text/javascript"> (function() { // superagent is exposed as `window.superagent` // if you wish to use "request" instead please // uncomment the following line of code: // `window.request = superagent;` superagent .post('/api/pet') .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body .set(
Bundler
If you are using browserify, webpack, rollup, or another bundler, then you can follow the same usage as Node above.
Browser behaviour changed to match Node when serializing application/x-www-form-urlencoded, using arrayFormat: 'indices' semantics of qs library. (See: https://www.npmjs.com/package/qs#stringifying)
We've implemented the build setup of Lass to simplify our stack and linting
Unminified browserified build size has been reduced from 48KB to 20KB (via tinyify and the latest version of Babel using @babel/preset-env and .browserslistrc)
Linting support has been added using caniuse-lite and eslint-plugin-compat
We can now target what versions of Node we wish to support more easily using .babelrc
If you use .parse() in the browser version, rename it to .serialize().
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).
If you use .then() in Internet Explorer, ensure that you have a polyfill that adds a global Promise object.
0.x to 1.x:
Instead of 1-argument callback .end(function(res){}) use .then(res => {}).