customizeAPIRequest(customizeAPIRequestFunction: Function)
[starting from version: 2.8]
This API call allows customizing the request before it is sent to a server. Only for "api"
data source type.
customizeAPIRequest
can be defined in two ways:
flexmonster.customizeAPIRequest(customizeAPIRequestFunction)
;new Flexmonster({customizeAPIRequest: customizeAPIRequestFunction, ...})
Parameters
customizeAPIRequestFunction
or null
in case you do not need to change anything. The customizeAPIRequestFunction
function has the following signature: customizeAPIRequestFunction(requestBody: Object): Object.
Data passed to customizeAPIRequestFunction
:
requestBody
– Object. The object containing the request’s body and through which the request can be customized. Has the following properties:requestHeaders
– Object. Allows you to add custom request headers. This object consists of "key": "value"
pairs, where "key"
is a header’s name and "value"
is its value. The requestHeaders
object is either empty or containing the request headers added in the Data Source Object in the report. After customization, Flexmonster adds the contents of requestHeaders
to the headers of the request. Changes made in requestBody
are added to the request’s body.The customizeAPIRequestFunction
function must return requestBody
, the changed object with new or updated request headers. If requestBody
is null
, custom request headers will not be added to the request.
Example
Adding custom request headers:
function customizeAPIRequestFunction(req) {
req.requestHeaders = {
"customHeader1": "This is the first custom request header",
"customHeader2": "This is the second custom request header"
};
console.log(req);
return(req);
}
Check out a live sample on JSFiddle.