Skip to content
This repository was archived by the owner on Mar 25, 2020. It is now read-only.

Request

Johan Sundén edited this page Oct 30, 2017 · 6 revisions

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', {
                format: 'json',
                data: {
                    foo: "bar"
                }    
            }, response => console.log(response))
        }
    }

- or URL in params -

    class Agent extends Dashboard.Agent {
        constructor() {
            this.request(
            {
                url: 'YOUR_ENDPOINT',
                data: {
                    foo: "bar"
                }    
            }, 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',
                data: {
                    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))
        }
    }

Clone this wiki locally