Home Manual Reference Source
import HttpRequest from 'ievv_jsbase/lib/http/HttpRequest'
public class | source

HttpRequest

Indirect Subclass:

HttpDjangoFileRequest

API for performing HTTP requests.

Example:

Make a POST request
const request = new HttpRequest('http://example.com/api/users/')
request.post('Hello world')
    .then((response) => {
        // Success - response is a HttpResponse object.
        console.log(response.toString())
        if(response.isSuccess()) {
            console.log('Success: ', response.body)
        } else if (response.isRedirect) {
            console.log('Hmm strange, we got a redirect instead of a 2xx response.')
        }
    })
    .catch((error) => {
        // Error - response is an HttpResponse object.
        console.error(error.toString())
        if(error.response.isRedirect()) {
            // Yes - redirect is treated as an error by default.
            // you can change this by supplying an extra argument
            // to HttpResponse()
            console.log('We got a 3xx response!', error.response.body)
        } else if(error.response.isClientError()) {
            console.log('We got a 4xx response!', error.response.body)
        } else if (error.response.isServerError()) {
            console.log('We got a 5xx response!', error.response.body)
        } else if (error.response.isConnectionRefused()) {
            console.log('Connection refused.')
        }
        // throw error  // You can throw the error as an exception
    })
Make a GET request with a querystring
const request = new HttpRequest('http://example.com/api/users/')
request.urlParser.queryString.set('search', 'doe')
request.get()
    .then((response) => {
        console.log('Success!', response.toString())
    })
    .catch((error) => {
        console.error('Error:', error.toString())
    })

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

request: *

public
public get

Get the parsed URL of the request.

Method Summary

Public Methods
public

deepCopy(): *

Create a deep copy of this HttpRequest object.

public

get(data: *): *

Shortcut for send("get", data).

public

head(data: *): *

Shortcut for send("head", data).

public

httpdelete(data: *): *

Shortcut for send("delete", data).

public

makeRequestBody(data: *): *

Make request body from the provided data.

public

Creates a HttpResponse.

public

patch(data: *): *

Shortcut for send("patch", data).

public

post(data: *): *

Shortcut for send("post", data).

public

put(data: *): *

Shortcut for send("put", data).

public

send(method: *, data: *): Promise

Send the request.

public

Set default request headers.

public

setRequestHeader(header: *, value: *)

Set a request header.

public

setTreatRedirectResponseAsError(treatRedirectResponseAsError: bool)

Set how we treat 3xx responses.

public

setUrl(url: String)

Set the URL of the request.

Public Constructors

public constructor(url: string) source

Params:

NameTypeAttributeDescription
url string

The URL to request. If this is supplied, it is passed to HttpRequest#setUrl

Public Members

public request: * source

public requestHeaders: * source

public get urlParser: UrlParser: * source

Get the parsed URL of the request.

Return:

UrlParser

The UrlParser for the parsed URL.

Public Methods

public deepCopy(): * source

Create a deep copy of this HttpRequest object.

WARNING: This does not copy request headers since those are set on the XMLHttpRequest object, and that object is reset in the copy.

Return:

*

The copy.

public get(data: *): * source

Shortcut for send("get", data).

Params:

NameTypeAttributeDescription
data *

Return:

*

See:

public head(data: *): * source

Shortcut for send("head", data).

Params:

NameTypeAttributeDescription
data *

Return:

*

See:

public httpdelete(data: *): * source

Shortcut for send("delete", data).

Named httpdelete to avoid crash with builtin keyword delete.

Params:

NameTypeAttributeDescription
data *

Return:

*

See:

public makeRequestBody(data: *): * source

Make request body from the provided data.

By default this just returns the provided data, but subclasses can override this to perform automatic conversion.

Must return a string.

Params:

NameTypeAttributeDescription
data *

Return:

*

public makeResponse(): HttpResponse source

Creates a HttpResponse.

Return:

HttpResponse

public patch(data: *): * source

Shortcut for send("patch", data).

Params:

NameTypeAttributeDescription
data *

Return:

*

See:

public post(data: *): * source

Shortcut for send("post", data).

Params:

NameTypeAttributeDescription
data *

Return:

*

See:

public put(data: *): * source

Shortcut for send("put", data).

Params:

NameTypeAttributeDescription
data *

Return:

*

See:

public send(method: *, data: *): Promise source

Send the request.

Params:

NameTypeAttributeDescription
method *

The HTTP method. I.e.: "get", "post", ...

data *

Request body data. This is sent through HttpRequest#makeRequestBody before it is sent.*

Return:

Promise

A Promise.

 The resolve function argument is an
 an object of whatever <span><a href="class/source/http/HttpRequest.js~HttpRequest.html#instance-method-makeResponse">HttpRequest#makeResponse</a></span>
 returns.

 The reject function argument is a
 <span><a href="variable/index.html#static-variable-HttpResponseError">HttpResponseError</a></span> object created using
 <span><a href="class/source/http/HttpResponse.js~HttpResponse.html#instance-method-toError">HttpResponse#toError</a></span>.

public setDefaultRequestHeaders(method: *) source

Set default request headers.

Does nothing by default, but subclasses can override this.

Params:

NameTypeAttributeDescription
method *

The HTTP request method (GET, POST, PUT, ...). Will always be uppercase.

public setRequestHeader(header: *, value: *) source

Set a request header.

Params:

NameTypeAttributeDescription
header *

The header name. E.g.: "Content-type".

value *

The header value.

public setTreatRedirectResponseAsError(treatRedirectResponseAsError: bool) source

Set how we treat 3xx responses.

By default they are treated as errors, but you can change this behavior by calling this function.

Params:

NameTypeAttributeDescription
treatRedirectResponseAsError bool

Treat 3xx responses as errors?

Example:

Do not treat 3xx responses as error
const request = HttpRequest('http://example.com/api/')
request.setTreatRedirectResponseAsError(false)

public setUrl(url: String) source

Set the URL of the request.

Params:

NameTypeAttributeDescription
url String

The URL.