API Reference

Class List

pc.Http

Used to send and receive HTTP requests.

Summary

Methods

delPerform an HTTP DELETE request to the given url
getPerform an HTTP GET request to the given url.
postPerform an HTTP POST request to the given url
putPerform an HTTP PUT request to the given url
requestMake a general purpose HTTP request.

Details

Constructor

Http()

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

Methods

del(url, [options], callback)

Perform an HTTP DELETE request to the given url

Parameters

urlObjectThe URL to make the request to
optionsObjectAdditional options
options.headersObjectHTTP headers to add to the request
options.asyncBooleanMake the request asynchronously (default: true)
options.cacheObjectIf false, then add a timestamp to the request to prevent caching
options.withCredentialsBooleanSend cookies with this request (default: true)
options.responseTypeStringOverride the response type
options.postdataDocument, ObjectData 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.cacheObjectIf false, then add a timestamp to the request to prevent caching
callbackfunctionThe 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.

get(url, [options], callback)

Perform an HTTP GET request to the given url.

pc.http.get("http://example.com/", function (err, response) {
    console.log(response);
});

Parameters

urlString
optionsObjectAdditional options
options.headersObjectHTTP headers to add to the request
options.asyncBooleanMake the request asynchronously (default: true)
options.cacheObjectIf false, then add a timestamp to the request to prevent caching
options.withCredentialsBooleanSend cookies with this request (default: true)
options.responseTypeStringOverride the response type
options.postdataDocument, ObjectData 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.cacheObjectIf false, then add a timestamp to the request to prevent caching
callbackfunctionThe 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.

post(url, [options], data, callback)

Perform an HTTP POST request to the given url

Parameters

urlStringThe URL to make the request to
optionsObjectAdditional options
options.headersObjectHTTP headers to add to the request
options.asyncBooleanMake the request asynchronously (default: true)
options.cacheObjectIf false, then add a timestamp to the request to prevent caching
options.withCredentialsBooleanSend cookies with this request (default: true)
options.responseTypeStringOverride the response type
options.cacheObjectIf false, then add a timestamp to the request to prevent caching
dataObjectData 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
callbackfunctionThe 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.

put(url, [options], data, callback)

Perform an HTTP PUT request to the given url

Parameters

urlStringThe URL to make the request to
optionsObjectAdditional options
options.headersObjectHTTP headers to add to the request
options.asyncBooleanMake the request asynchronously (default: true)
options.cacheObjectIf false, then add a timestamp to the request to prevent caching
options.withCredentialsBooleanSend cookies with this request (default: true)
options.responseTypeStringOverride the response type
options.cacheObjectIf false, then add a timestamp to the request to prevent caching
dataDocument, ObjectData 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
callbackfunctionThe 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.

request(method, url, [options], callback)

Make a general purpose HTTP request.

Parameters

methodStringThe HTTP method "GET", "POST", "PUT", "DELETE"
urlStringThe url to make the request to
optionsObjectAdditional options
options.headersObjectHTTP headers to add to the request
options.asyncBooleanMake the request asynchronously (default: true)
options.cacheObjectIf false, then add a timestamp to the request to prevent caching
options.withCredentialsBooleanSend cookies with this request (default: true)
options.responseTypeStringOverride the response type
options.postdataDocument, ObjectData 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.cacheObjectIf false, then add a timestamp to the request to prevent caching
callbackfunctionThe callback used when the response has retured. Passed (err, data) where data is the response (format depends on response type, text, Object, ArrayBuffer, XML) and err is the error code.