This repository was archived by the owner on Mar 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Request
Johan Sundén edited this page May 4, 2017
·
6 revisions
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
class Agent extends Dashboard.Agent {
constructor() {
this.request('YOUR_ENDPOINT', data => console.log(data))
}
}
class Agent extends Dashboard.Agent {
constructor() {
this.request('YOUR_ENDPOINT', {
format: 'json',
data: {
foo: "bar"
}
}, response => console.log(response))
}
}
class Agent extends Dashboard.Agent {
constructor() {
this.request(
{
url: 'YOUR_ENDPOINT',
data: {
foo: "bar"
}
}, 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 that yourself and parse as something else than json promise
is more suitable.
class Agent extends Dashboard.Agent {
constructor() {
this.request('YOUR_ENDPOINT').then(response => response.text()).then(response => console.log(response))
}
}
Following examples uses callbacks but it works perfectly fine to use promises instead.
class Agent extends Dashboard.Agent {
constructor() {
this.request({
url: 'YOUR_ENDPOINT',
method: 'POST',
data: {
foo: "bar"
}
}, response => console.log(response))
}
}
class Agent extends Dashboard.Agent {
constructor() {
this.request({
url: 'YOUR_ENDPOINT',
headers: {
"x-request-params": "foobar"
}
}, response => console.log(response))
}
}