Skip to main content

RequestOptions

Allows you to customize various aspects of the HTTP request, such as headers, body content, and handling of retries and timeouts.

Properties:

headers?

An optional object representing the headers to be included in the HTTP request. Headers provide additional information about the request, such as content type, authentication tokens, or custom metadata.

Example:

const options: RequestOptions = {
headers: { 'Authorization': 'Bearer token' }
};

json?

An optional object that represents the JSON payload to be sent in the body of the request. Use this when you need to send structured data.

Example:

const options: RequestOptions = {
json: { name: 'John', age: 30 }
};

retry?

An optional number indicating how many times the request should be retried in case of failure. Useful for handling transient errors or network issues.

Example:

const options: RequestOptions = { retry: 3 };

timeout?

An optional number specifying the maximum time in milliseconds to wait for the request to complete before timing out.

Example:

const options: RequestOptions = { timeout: 5000 };

body?

Represents the data to be sent with an HTTP request. Can accept various formats, including FormData and URLSearchParams.

Example:

const options: RequestOptions = { body: new FormData() };

HttpRequest

Provides a simple and adaptable way to create and handle HTTP requests. Use RequestOptions to configure each request.

Methods:

static get(url: string, options?: RequestOptions): ResponsePromise

Sends a GET request to the specified URL.

static post(url: string, options?: RequestOptions): ResponsePromise

Sends a POST request to the specified URL.

static put(url: string, options?: RequestOptions): ResponsePromise

Sends a PUT request to the specified URL.

static patch(url: string, options?: RequestOptions): ResponsePromise

Sends a PATCH request to the specified URL.

static head(url: string, options?: RequestOptions): ResponsePromise

Sends a HEAD request to the specified URL.

static delete(url: string, options?: RequestOptions): ResponsePromise

Sends a DELETE request to the specified URL.

Example:

const response = await HttpRequest.get('https://api.example.com/data', { headers: { 'Authorization': 'Bearer token' } });
const data = await response.json();