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

JsonHttpRequest

Extends:

HttpRequest → JsonHttpRequest

Extends HttpRequest with transparent JSON request/response handling.

Example:

Make a GET request
const request = new JsonHttpRequest('http://example.com/api/users/')
request.urlParser.queryString.set('search', 'doe')
request.get()
    .then((response) => {
        console.log('Success!', response.bodydata)
    })
    .catch((error) => {
        console.error('Error:', error.toString())
    })

Method Summary

Public Methods
public

getAllPaginationPages(resultsAttribute: string, nextUrlAttribute: string, maxPages: number): Promise<any>

Shortcut for getting all pagination pages and resolve an array of the results for all pages.

public

makeRequestBody(data: *): *

Overridden to automatically convert request data to JSON.

public

Overridden to return the response as a HttpJsonResponse instead if a HttpResponse.

public

Overridden to ensure we send the correct content-type header for json requests.

Inherited Summary

From class HttpRequest
public get

Get the parsed URL of the request.

public

request: *

public
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 Methods

public getAllPaginationPages(resultsAttribute: string, nextUrlAttribute: string, maxPages: number): Promise<any> source

Shortcut for getting all pagination pages and resolve an array of the results for all pages.

Assumes your API uses pagination where the next page is returned as an URL, and that the results for each pagination page is an array.

The first page retrived is whatever this request is configured for, so you can use this to do things like request pagination page 10 -> 20, just ensure the request is configured so that the page that would have been retrieved if you just used get() would have been page 10.

Params:

NameTypeAttributeDescription
resultsAttribute string

The attribute in the response data that contains the results array. Defaults to results.

nextUrlAttribute string

The attribute in the response data that contains the URL of the next pagination page. Defaults to next.

maxPages number

The max number of pages to load. If this is null (the default) we load all pages until the nextUrlAttribute value is null or undefined.

Return:

Promise<any>

public makeRequestBody(data: *): * source

Overridden to automatically convert request data to JSON.

Override:

HttpRequest#makeRequestBody

Params:

NameTypeAttributeDescription
data *

Return:

*

public makeResponse(): HttpJsonResponse source

Overridden to return the response as a HttpJsonResponse instead if a HttpResponse.

Override:

HttpRequest#makeResponse

public setDefaultRequestHeaders(method: *) source

Overridden to ensure we send the correct content-type header for json requests.

Override:

HttpRequest#setDefaultRequestHeaders

Params:

NameTypeAttributeDescription
method *