request

request is a proxy request to come around CORS issues.

Both callbacks and promises is supported so it is all about which approach you will want to take.

How to use

(as callback)

import { Plugin } from'@root'

Plugin.request('YOUR_ENDPOINT', data => console.log(data))

params (as callback)

import { Plugin } from'@root'

Plugin.request('YOUR_ENDPOINT', {
    json: true
}, response => console.log(response))

// OR url in params
Plugin.request({
    url: 'YOUR_ENDPOINT',
    json: true
}, response => console.log(response))

When working with callbacks the request component will always try to parse the response as json.

If you want to handle the parsing by yourself and parse as something else than json promise is more suitable.

(as promise)

import { Plugin } from'@root'

Plugin.request('YOUR_ENDPOINT')
    .then(response => response.text())
    .then(response => console.log(response))

GET is the default request method.

Advanced examples

Pluginrequest({
    url: 'YOUR_ENDPOINT',
    method: 'POST',
    formData {
        foo: 'bar'
    }
}, response => console.log(response))

// ============= //

Plugin.request({
    url: 'YOUR_ENDPOINT',
    headers: {
        "x-request-params": 'foobar'
    }
}, response => console.log(response))

Last updated