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
Rickard Hansson edited this page Sep 14, 2018
·
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", {
json: true
}, response => console.log(response));
}
}class Agent extends Dashboard.Agent {
constructor() {
this.request({
url: 'YOUR_ENDPOINT'
}, 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',
formData {
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));
}
}