Elegant & feature rich browser / node HTTP with a fluent API
—
Core HTTP method functions for making requests with fluent chainable interface. All methods return a Request instance that can be configured and executed.
Performs HTTP GET requests for retrieving data.
/**
* Create a GET request
* @param {string} url - Request URL
* @param {object|function} [data] - Query parameters or callback function
* @param {function} [callback] - Callback function
* @returns {Request} Request instance for chaining
*/
function get(url, data?, callback?): Request;Usage Examples:
const superagent = require('superagent');
// Basic GET request
superagent.get('https://api.example.com/users');
// GET with query parameters
superagent.get('https://api.example.com/users', { page: 1, limit: 10 });
// GET with callback shorthand
superagent.get('https://api.example.com/users', (err, res) => {
console.log(res.body);
});Performs HTTP POST requests for creating or submitting data.
/**
* Create a POST request
* @param {string} url - Request URL
* @param {any} [data] - Request body data or callback function
* @param {function} [callback] - Callback function
* @returns {Request} Request instance for chaining
*/
function post(url, data?, callback?): Request;Usage Examples:
// POST with JSON data
superagent
.post('https://api.example.com/users')
.send({ name: 'John', email: 'john@example.com' });
// POST with form data
superagent
.post('https://api.example.com/login')
.type('form')
.send({ username: 'user', password: 'pass' });Performs HTTP PUT requests for updating or replacing resources.
/**
* Create a PUT request
* @param {string} url - Request URL
* @param {any} [data] - Request body data or callback function
* @param {function} [callback] - Callback function
* @returns {Request} Request instance for chaining
*/
function put(url, data?, callback?): Request;Performs HTTP PATCH requests for partial updates to resources.
/**
* Create a PATCH request
* @param {string} url - Request URL
* @param {any} [data] - Request body data or callback function
* @param {function} [callback] - Callback function
* @returns {Request} Request instance for chaining
*/
function patch(url, data?, callback?): Request;Performs HTTP DELETE requests for removing resources. Available as both delete and del.
/**
* Create a DELETE request
* @param {string} url - Request URL
* @param {any} [data] - Request body data or callback function
* @param {function} [callback] - Callback function
* @returns {Request} Request instance for chaining
*/
function delete(url, data?, callback?): Request;
function del(url, data?, callback?): Request; // Alias for deletePerforms HTTP HEAD requests for retrieving headers without response body.
/**
* Create a HEAD request
* @param {string} url - Request URL
* @param {any} [data] - Query parameters or callback function
* @param {function} [callback] - Callback function
* @returns {Request} Request instance for chaining
*/
function head(url, data?, callback?): Request;Performs HTTP OPTIONS requests for retrieving allowed methods and CORS preflight.
/**
* Create an OPTIONS request
* @param {string} url - Request URL
* @param {any} [data] - Request body data or callback function
* @param {function} [callback] - Callback function
* @returns {Request} Request instance for chaining
*/
function options(url, data?, callback?): Request;Creates requests with custom HTTP methods.
/**
* Create a request with custom method
* @param {string} method - HTTP method
* @param {string} url - Request URL
* @returns {Request} Request instance for chaining
*/
function request(method, url): Request;
// Shorthand syntax
function request(url): Request; // Defaults to GET
function request(url, callback): Request; // GET with callbackUsage Examples:
// Custom HTTP method
superagent('PROPFIND', 'https://webdav.example.com/folder/');
// Default GET
superagent('https://api.example.com/users');
// GET with callback
superagent('https://api.example.com/users', (err, res) => {
console.log(res.body);
});All methods support callback shortcuts for immediate execution:
// Long form
superagent.get('https://api.example.com/users').end((err, res) => {
// handle response
});
// Shortcut
superagent.get('https://api.example.com/users', (err, res) => {
// handle response
});Methods automatically set appropriate Content-Type headers:
application/jsonapplication/x-www-form-urlencodedtext/plain (unless otherwise specified)application/octet-streamInstall with Tessl CLI
npx tessl i tessl/npm-superagent