SuperAgent
Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemai...
README
superagent
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.
Table of Contents
Install
[npm][]:
- ```sh
- npm install superagent
- ```
[yarn][]:
- ```sh
- yarn add superagent
- ```
Usage
Node
- ```js
- const superagent = require('superagent');
- // callback
- superagent
- .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/catch
- superagent.post('/api/pet').then(console.log).catch(console.error);
- // promise with async/await
- (async () => {
- try {
- const res = await superagent.post('/api/pet');
- console.log(res);
- } catch (err) {
- console.error(err);
- }
- })();
- ```
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.