JsonHttpRequest
Extends:
Extends HttpRequest with transparent JSON request/response handling.
Example:
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 |
setDefaultRequestHeaders(method: *) 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 |
|
public |
head(data: *): * Shortcut for |
|
public |
httpdelete(data: *): * Shortcut for |
|
public |
makeRequestBody(data: *): * Make request body from the provided data. |
|
public |
Creates a HttpResponse. |
|
public |
patch(data: *): * Shortcut for |
|
public |
post(data: *): * Shortcut for |
|
public |
put(data: *): * Shortcut for |
|
public |
Send the request. |
|
public |
setDefaultRequestHeaders(method: *) Set default request headers. |
|
public |
setRequestHeader(header: *, value: *) Set a request header. |
|
public |
setTreatRedirectResponseAsError(treatRedirectResponseAsError: bool) Set how we treat 3xx responses. |
|
public |
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:
Name | Type | Attribute | Description |
resultsAttribute | string | The attribute in the response data that
contains the results array. Defaults to |
|
nextUrlAttribute | string | The attribute in the response data that
contains the URL of the next pagination page. Defaults to |
|
maxPages | number | The max number of pages to load. If this is |
public makeRequestBody(data: *): * source
Overridden to automatically convert request data to JSON.
Override:
HttpRequest#makeRequestBodyParams:
Name | Type | Attribute | Description |
data | * |
Return:
* |
public makeResponse(): HttpJsonResponse source
Overridden to return the response as a HttpJsonResponse instead if a HttpResponse.
Override:
HttpRequest#makeResponsepublic setDefaultRequestHeaders(method: *) source
Overridden to ensure we send the correct content-type header for json requests.
Override:
HttpRequest#setDefaultRequestHeadersParams:
Name | Type | Attribute | Description |
method | * |