Request - Infomaker/Dashboard-Plugin GitHub Wiki

Request

Agents can make proxy request to come around CORS issues with the request function. Both callbacks and promises is supported so it is all about which approach you will want to take.


💁🏻 GET is the default request method.


Example (with callback)

class Agent extends Dashboard.Agent {
    constructor() {
        this.request("YOUR_ENDPOINT", data => console.log(data));
    }
}

Example with params (with callback)

class Agent extends Dashboard.Agent {
    constructor() {
        this.request("YOUR_ENDPOINT", {
            json: true
        }, response => console.log(response));
    }
}

- or URL in params -

class Agent extends Dashboard.Agent {
    constructor() {
        this.request({
            url: 'YOUR_ENDPOINT'
        }, response => console.log(response))
    }
}

Callback responses


💁🏻 When working with callbacks the request component will always try to parse the response as json. If you want to handle that yourself and parse as something else than json promise is more suitable.


Example (with promise)

class Agent extends Dashboard.Agent {
    constructor() {
        this.request("YOUR_ENDPOINT")
            .then(response => response.text())
            .then(response => console.log(response));
    }
}

More advanced examples

Following examples uses callbacks but it works perfectly fine to use promises instead.

POST example

class Agent extends Dashboard.Agent {
    constructor() {
        this.request({
            url: 'YOUR_ENDPOINT',
            method: 'POST',
            formData {
                foo: "bar"
            }
        }, response => console.log(response))
    }
}

Custom headers example

class Agent extends Dashboard.Agent {
    constructor() {
        this.request({
            url: "YOUR_ENDPOINT",
            headers: {
                "x-request-params": "foobar"
            }
        }, response => console.log(response));
    }
}