CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-superagent

Elegant & feature rich browser / node HTTP with a fluent API

Pending
Overview
Eval results
Files

http-methods.mddocs/

HTTP Methods

Core HTTP method functions for making requests with fluent chainable interface. All methods return a Request instance that can be configured and executed.

Capabilities

GET Method

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);
});

POST Method

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' });

PUT Method

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;

PATCH Method

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;

DELETE Method

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 delete

HEAD Method

Performs 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;

OPTIONS Method

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;

Generic Request Method

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 callback

Usage 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);
});

Method Behavior

Data Parameter Handling

  • GET/HEAD: Data is converted to query parameters
  • POST/PUT/PATCH/DELETE: Data is sent as request body
  • Function as data: Treated as callback function

Callback Shortcuts

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
});

Automatic Content-Type

Methods automatically set appropriate Content-Type headers:

  • JSON data: application/json
  • Form data: application/x-www-form-urlencoded
  • String data: text/plain (unless otherwise specified)
  • Buffer data: application/octet-stream

Install with Tessl CLI

npx tessl i tessl/npm-superagent

docs

agent-sessions.md

auth-security.md

file-uploads.md

http-methods.md

index.md

parsers-serializers.md

request-building.md

request-execution.md

response-handling.md

tile.json