# Http

Class · category: Framework

Source: https://github.com/playcanvas/engine/blob/970d89f6d3bc6667f4e1f88153abd6e7e82ed52c/src/platform/net/http.js#L28

Used to send and receive HTTP requests.

## Methods

### del

```ts
del(url: string, options: object, callback: HttpResponseCallback): XMLHttpRequest
```

Perform an HTTP DELETE request to the given url with additional options such as headers,
retries, credentials, etc.

**Parameters**

- `url` (`string`): The URL to make the request to.
- `options` (`object`): Additional options.
    - `options.async` (`boolean`, optional): Make the request asynchronously. Defaults to true.
    - `options.cache` (`boolean`, optional): If false, then add a timestamp to the request to prevent caching.
    - `options.headers` (`{}`, optional): HTTP headers to add to the request.
    - `options.maxRetries` (`number`, optional): If options.retry is true this specifies the maximum
      number of retries. Defaults to 5.
    - `options.maxRetryDelay` (`number`, optional): If options.retry is true this specifies the
      maximum amount of time to wait between retries in milliseconds. Defaults to 5000.
    - `options.postdata` (`any`, optional): Data to send in the body of the request.
      Some content types are handled automatically. If postdata is an XML Document, it is handled.
      If the Content-Type header is set to 'application/json' then the postdata is JSON
      stringified. Otherwise, by default, the data is sent as form-urlencoded.
    - `options.responseType` (`string`, optional): Override the response type.
    - `options.retry` (`boolean`, optional): If true then if the request fails it will be retried with
      an exponential backoff.
    - `options.withCredentials` (`boolean`, optional): Send cookies with this request. Defaults to false.
- `callback` ([`HttpResponseCallback`](https://api.playcanvas.com/engine/types/HttpResponseCallback.md)): The callback used when the response has returned.
  Passed (err, data) where data is the response (format depends on response type: text,
  Object, ArrayBuffer, XML) and err is the error code.

**Returns** `XMLHttpRequest`: The request object.

**Example**

```ts
http.del("http://example.com/", {
    "retry": true,
    "maxRetries": 5
}, (err, response) => {
    console.log(response);
});
```

### get

```ts
get(url: string, options: object, callback: HttpResponseCallback): XMLHttpRequest
```

Perform an HTTP GET request to the given url with additional options such as headers,
retries, credentials, etc.

**Parameters**

- `url` (`string`): The URL to make the request to.
- `options` (`object`): Additional options.
    - `options.async` (`boolean`, optional): Make the request asynchronously. Defaults to true.
    - `options.cache` (`boolean`, optional): If false, then add a timestamp to the request to prevent caching.
    - `options.headers` (`{}`, optional): HTTP headers to add to the request.
    - `options.maxRetries` (`number`, optional): If options.retry is true this specifies the maximum number of retries. Defaults to 5.
    - `options.maxRetryDelay` (`number`, optional): If options.retry is true this specifies the maximum amount of time to wait between retries in milliseconds. Defaults to 5000.
    - `options.postdata` (`any`, optional): Data to send in the body of the request.
      Some content types are handled automatically. If postdata is an XML Document, it is handled. If
      the Content-Type header is set to 'application/json' then the postdata is JSON stringified.
      Otherwise, by default, the data is sent as form-urlencoded.
    - `options.progress` ([`EventHandler`](https://api.playcanvas.com/engine/classes/EventHandler.md), optional): Object to use for firing progress events.
    - `options.responseType` (`string`, optional): Override the response type.
    - `options.retry` (`boolean`, optional): If true then if the request fails it will be retried with an exponential backoff.
    - `options.withCredentials` (`boolean`, optional): Send cookies with this request. Defaults to false.
- `callback` ([`HttpResponseCallback`](https://api.playcanvas.com/engine/types/HttpResponseCallback.md)): The callback used when the response has returned. Passed (err, data)
  where data is the response (format depends on response type: text, Object, ArrayBuffer, XML) and
  err is the error code.

**Returns** `XMLHttpRequest`: The request object.

**Example**

```ts
http.get("http://example.com/", {
    "retry": true,
    "maxRetries": 5
}, (err, response) => {
    console.log(response);
});
```

### post

```ts
post(url: string, data: any, options: object, callback: HttpResponseCallback): XMLHttpRequest
```

Perform an HTTP POST request to the given url with additional options such as headers,
retries, credentials, etc.

**Parameters**

- `url` (`string`): The URL to make the request to.
- `data` (`any`): Data to send in the body of the request.
  Some content types are handled automatically. If postdata is an XML Document, it is handled.
  If the Content-Type header is set to 'application/json' then the postdata is JSON
  stringified. Otherwise, by default, the data is sent as form-urlencoded.
- `options` (`object`): Additional options.
    - `options.async` (`boolean`, optional): Make the request asynchronously. Defaults to true.
    - `options.cache` (`boolean`, optional): If false, then add a timestamp to the request to prevent caching.
    - `options.headers` (`{}`, optional): HTTP headers to add to the request.
    - `options.maxRetries` (`number`, optional): If options.retry is true this specifies the maximum
      number of retries. Defaults to 5.
    - `options.maxRetryDelay` (`number`, optional): If options.retry is true this specifies the
      maximum amount of time to wait between retries in milliseconds. Defaults to 5000.
    - `options.responseType` (`string`, optional): Override the response type.
    - `options.retry` (`boolean`, optional): If true then if the request fails it will be retried with an exponential backoff.
    - `options.withCredentials` (`boolean`, optional): Send cookies with this request. Defaults to false.
- `callback` ([`HttpResponseCallback`](https://api.playcanvas.com/engine/types/HttpResponseCallback.md)): The callback used when the response has returned.
  Passed (err, data) where data is the response (format depends on response type: text,
  Object, ArrayBuffer, XML) and err is the error code.

**Returns** `XMLHttpRequest`: The request object.

**Example**

```ts
http.post("http://example.com/", {
    "name": "Alex"
}, {
    "retry": true,
    "maxRetries": 5
}, (err, response) => {
    console.log(response);
});
```

### put

```ts
put(url: string, data: any, options: object, callback: HttpResponseCallback): XMLHttpRequest
```

Perform an HTTP PUT request to the given url with additional options such as headers,
retries, credentials, etc.

**Parameters**

- `url` (`string`): The URL to make the request to.
- `data` (`any`): Data to send in the body of the request. Some content types
  are handled automatically. If postdata is an XML Document, it is handled. If the
  Content-Type header is set to 'application/json' then the postdata is JSON stringified.
  Otherwise, by default, the data is sent as form-urlencoded.
- `options` (`object`): Additional options.
    - `options.async` (`boolean`, optional): Make the request asynchronously. Defaults to true.
    - `options.cache` (`boolean`, optional): If false, then add a timestamp to the request to prevent caching.
    - `options.headers` (`{}`, optional): HTTP headers to add to the request.
    - `options.maxRetries` (`number`, optional): If options.retry is true this specifies the maximum
      number of retries. Defaults to 5.
    - `options.maxRetryDelay` (`number`, optional): If options.retry is true this specifies the
      maximum amount of time to wait between retries in milliseconds. Defaults to 5000.
    - `options.responseType` (`string`, optional): Override the response type.
    - `options.retry` (`boolean`, optional): If true then if the request fails it will be retried with
      an exponential backoff.
    - `options.withCredentials` (`boolean`, optional): Send cookies with this request. Defaults to false.
- `callback` ([`HttpResponseCallback`](https://api.playcanvas.com/engine/types/HttpResponseCallback.md)): The callback used when the response has returned.
  Passed (err, data) where data is the response (format depends on response type: text,
  Object, ArrayBuffer, XML) and err is the error code.

**Returns** `XMLHttpRequest`: The request object.

**Example**

```ts
http.put("http://example.com/", {
    "name": "Alex"
}, {
    "retry": true,
    "maxRetries": 5
}, (err, response) => {
    console.log(response);
});
```

### request

```ts
request(method: string, url: string, options: object, callback: HttpResponseCallback): XMLHttpRequest
```

Make a general purpose HTTP request with additional options such as headers, retries,
credentials, etc.

**Parameters**

- `method` (`string`): The HTTP method "GET", "POST", "PUT", "DELETE".
- `url` (`string`): The url to make the request to.
- `options` (`object`): Additional options.
    - `options.async` (`boolean`, optional): Make the request asynchronously. Defaults to true.
    - `options.cache` (`boolean`, optional): If false, then add a timestamp to the request to prevent caching.
    - `options.headers` (`{}`, optional): HTTP headers to add to the request.
    - `options.maxRetries` (`number`, optional): If options.retry is true this specifies the maximum
      number of retries. Defaults to 5.
    - `options.maxRetryDelay` (`number`, optional): If options.retry is true this specifies the
      maximum amount of time to wait between retries in milliseconds. Defaults to 5000.
    - `options.postdata` (`any`, optional): Data to send in the body of the request.
      Some content types are handled automatically. If postdata is an XML Document, it is handled.
      If the Content-Type header is set to 'application/json' then the postdata is JSON
      stringified. Otherwise, by default, the data is sent as form-urlencoded.
    - `options.responseType` (`string`, optional): Override the response type.
    - `options.retry` (`boolean`, optional): If true then if the request fails it will be retried with
      an exponential backoff.
    - `options.withCredentials` (`boolean`, optional): Send cookies with this request. Defaults to false.
- `callback` ([`HttpResponseCallback`](https://api.playcanvas.com/engine/types/HttpResponseCallback.md)): The callback used when the response has returned.
  Passed (err, data) where data is the response (format depends on response type: text,
  Object, ArrayBuffer, XML) and err is the error code.

**Returns** `XMLHttpRequest`: The request object.

**Example**

```ts
http.request("get", "http://example.com/", {
    "retry": true,
    "maxRetries": 5
}, (err, response) => {
    console.log(response);
});
```
