Skip to content

Request

Performs HTTP requests.

Performs a URL request and returns the response in an appropriate format.

url

URL to send request to.

url: string

method

HTTP method used for the request.

Specifies the HTTP method to use when sending the request. The default is to send the request using the GET HTTP method.

method: string

headers

HTTP headers to send with the request.

Key value pairs where the key is the name of an HTTP header and the value will be sent as the value for the HTTP header.

headers: {string: string}

body

Body to send with the request.

The body will be send along the request. While this property can be any value, currently only strings and Data is supported.

Be aware that this property is ignored if you convert the request to a multipart request using addParameterToMultipart, addFileToMultipart or addFileDataToMultipart.

body: any

onRedirect

Function called upon redirect.

The function determines how redirects should be handled. By default redirects are allowed. When invoked the function is supplied with the request that we're about to redirect to. The function can return the request to continue redirecting or it can return another request to redirect to. Returning null will stop the redirect. Note that onRedirect will only be invoked on the initial request. Consecutive redirects should be handled on the initial request.

onRedirect: fn(Request) -> Request

response

Response of the request.

The response is not populated until the request have been completed. The response is an object that looks like the following example.

{
  "url": "https://example.com/",
  "statusCode": 200
  "mimeType": "application/json",
  "textEncodingName": "utf-8",
  "headers": {
    "Content-Type": "application/json;charset=utf-8",
    "Content-Length": "17671"
  }
}
response: {string: any}

-new Request

Constructs a request.

Constructs a new request that will be sent to the provided URL. The request is not sent until an appropriate load method is called, e.g. loadImage for downloading and interpreting the response as an image.

new Request(url: string)

-load

Sends request.

Call to send the configured request to the specified URL. The raw response is provided when the returned promise is fulfilled.

load(): Promise<Data>

-loadString

Sends request and parses response as a string.

Call to send the configured request to the specified URL. The response is parsed to a string and provided when the returned promise is fulfilled.

loadString(): Promise<string>

-loadJSON

Sends request and parses response as JSON.

Call to send the configured request to the specified URL. The response is expected to be a valid JSON string and is parsed into an object.

loadJSON(): Promise<any>

-loadImage

Sends request and parses response as an image.

Call to send the configured request to the specified URL. The response is expected to be an image.

loadImage(): Promise<Image>

-addParameterToMultipart

Adds a parameter to a multipart request.

Converts the request to a multipart request and adds a parameter with the specified name and value. Be aware that the body property on the request is ignored for multipart requests as parameters and files added to the request constitutes the body.

Calling this function will make the request a multipart request. When the request is send, the content type will automatically be set to "multipart/form-data".

addParameterToMultipart(name: string, value: string)

-addFileDataToMultipart

Adds a file to a multipart request.

Converts the request to a multipart request and adds the file to the request. Be aware that the body property on the request is ignored for multipart requests as parameters and files added to the request constitutes the body.

Calling this function will make the request a multipart request. When the request is send, the content type will automatically be set to "multipart/form-data".

addFileDataToMultipart(data: Data, mimeType: string, name: string, filename: string)

-addFileToMultipart

Adds a file to a multipart request.

Converts the request to a multipart request and adds the file to the request. The function will automatically determine the MIME type of the file as well as the filename. Be aware that the body\ property on the request is ignored for multipart requests as\ parameters and files added to the request constitutes the body.

Calling this function will make the request a multipart request. When the request is send, the content type will automatically be set to "multipart/form-data".

addFileToMultipart(filePath: string, name: string, filename: string)

-addImageToMultipart

Adds an image to a multipart request.

Converts the request to a multipart request and adds the image to the request. The function will automatically determine the MIME type of the file Be aware that the body property on the request is ignored for multipart requests as parameters and files added to the request constitutes the body.

Calling this function will make the request a multipart request. When the request is send, the content type will automatically be set to "multipart/form-data".

addImageToMultipart(image: Image, name: string, filename: string)