Class: Http

pc.Http

Used to send and receive HTTP requests.

Constructor

new Http()

Create a new Http instance. By default, a PlayCanvas application creates an instance of this object at `pc.http`.
Source:

Methods

del(url, optionsopt, callback) → {XMLHttpRequest}

Perform an HTTP DELETE request to the given url
Parameters:
Name Type Attributes Description
url Object The URL to make the request to
options Object <optional>
Additional options
Properties
Name Type Attributes Description
headers Object <optional>
HTTP headers to add to the request
async Boolean <optional>
Make the request asynchronously. Defaults to true.
cache Object <optional>
If false, then add a timestamp to the request to prevent caching
withCredentials Boolean <optional>
Send cookies with this request. Defaults to true.
responseType String <optional>
Override the response type
postdata Document | Object <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.
retry Boolean <optional>
If true then if the request fails it will be retried with an exponential backoff.
maxRetries Number <optional>
If options.retry is true this specifies the maximum number of retries. Defaults to 5.
maxRetryDelay Number <optional>
If options.retry is true this specifies the maximum amount of time to wait between retries in milliseconds. Defaults to 5000.
callback function 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.
Source:
Returns:
The request object.
Type
XMLHttpRequest

get(url, optionsopt, callback) → {XMLHttpRequest}

Perform an HTTP GET request to the given url.
Parameters:
Name Type Attributes Description
url String The URL to make the request to.
options Object <optional>
Additional options
Properties
Name Type Attributes Description
headers Object <optional>
HTTP headers to add to the request
async Boolean <optional>
Make the request asynchronously. Defaults to true.
cache Object <optional>
If false, then add a timestamp to the request to prevent caching
withCredentials Boolean <optional>
Send cookies with this request. Defaults to true.
responseType String <optional>
Override the response type
postdata Document | Object <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.
retry Boolean <optional>
If true then if the request fails it will be retried with an exponential backoff.
maxRetries Number <optional>
If options.retry is true this specifies the maximum number of retries. Defaults to 5.
maxRetryDelay Number <optional>
If options.retry is true this specifies the maximum amount of time to wait between retries in milliseconds. Defaults to 5000.
callback function 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.
Source:
Returns:
The request object.
Type
XMLHttpRequest
Example
pc.http.get("http://example.com/", function (err, response) {
    console.log(response);
});

post(url, data, optionsopt, callback) → {XMLHttpRequest}

Perform an HTTP POST request to the given url.
Parameters:
Name Type Attributes Description
url String The URL to make the request to.
data Object 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 <optional>
Additional options
Properties
Name Type Attributes Description
headers Object <optional>
HTTP headers to add to the request
async Boolean <optional>
Make the request asynchronously. Defaults to true.
cache Object <optional>
If false, then add a timestamp to the request to prevent caching
withCredentials Boolean <optional>
Send cookies with this request. Defaults to true.
responseType String <optional>
Override the response type
retry Boolean <optional>
If true then if the request fails it will be retried with an exponential backoff.
maxRetries Number <optional>
If options.retry is true this specifies the maximum number of retries. Defaults to 5.
maxRetryDelay Number <optional>
If options.retry is true this specifies the maximum amount of time to wait between retries in milliseconds. Defaults to 5000.
callback function 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.
Source:
Returns:
The request object.
Type
XMLHttpRequest

put(url, data, optionsopt, callback) → {XMLHttpRequest}

Perform an HTTP PUT request to the given url.
Parameters:
Name Type Attributes Description
url String The URL to make the request to.
data Document | Object 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 <optional>
Additional options
Properties
Name Type Attributes Description
headers Object <optional>
HTTP headers to add to the request
async Boolean <optional>
Make the request asynchronously. Defaults to true.
cache Object <optional>
If false, then add a timestamp to the request to prevent caching
withCredentials Boolean <optional>
Send cookies with this request. Defaults to true.
responseType String <optional>
Override the response type
retry Boolean <optional>
If true then if the request fails it will be retried with an exponential backoff.
maxRetries Number <optional>
If options.retry is true this specifies the maximum number of retries. Defaults to 5.
maxRetryDelay Number <optional>
If options.retry is true this specifies the maximum amount of time to wait between retries in milliseconds. Defaults to 5000.
callback function 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.
Source:
Returns:
The request object.
Type
XMLHttpRequest

request(method, url, optionsopt, callback) → {XMLHttpRequest}

Make a general purpose HTTP request.
Parameters:
Name Type Attributes Description
method String The HTTP method "GET", "POST", "PUT", "DELETE"
url String The url to make the request to
options Object <optional>
Additional options
Properties
Name Type Attributes Description
headers Object <optional>
HTTP headers to add to the request
async Boolean <optional>
Make the request asynchronously. Defaults to true.
cache Object <optional>
If false, then add a timestamp to the request to prevent caching
withCredentials Boolean <optional>
Send cookies with this request. Defaults to true.
retry Boolean <optional>
If true then if the request fails it will be retried with an exponential backoff.
maxRetries Number <optional>
If options.retry is true this specifies the maximum number of retries. Defaults to 5.
maxRetryDelay Number <optional>
If options.retry is true this specifies the maximum amount of time to wait between retries in milliseconds. Defaults to 5000.
responseType String <optional>
Override the response type
postdata Document | Object <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.
callback function 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.
Source:
Returns:
The request object.
Type
XMLHttpRequest