Flipabit
Search…
Networking
A guide to work with network requests and responses. Using rest api with Flipabit.
The XMLHttpRequest object allows the user to register a response handle function and a url. A request can be send using one of the http verbs (get, post, put, delete, ...) to make the request. When the response arrive the handle function is called. The handle function is called several times. Every-time the request state has changed (for example headers have arrived or request is done).
Here a short example:
1
function request() {
2
var xhr = new XMLHttpRequest();
3
xhr.onreadystatechange = function() {
4
if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
5
print('HEADERS_RECEIVED');
6
} else if(xhr.readyState === XMLHttpRequest.DONE) {
7
print('DONE');
8
}
9
}
10
xhr.open("GET", "http://example.com");
11
xhr.send();
12
}
Copied!
Read more:

Examples

Zapier

Using Zapier Webhooks. Download the zapier.pma example.
2. Add following fields
3. Edit Template
4. Turn on your ZAP
5.Paste Webhook URL into code
The code:
1
var xhr = new XMLHttpRequest();
2
​
3
xhr.onreadystatechange = function() {
4
​
5
if(xhr.readyState === XMLHttpRequest.DONE) {
6
console.log("Status code is: " + xhr.status + " (" + xhr.statusText + ")")
7
if (xhr.status == 200) {
8
console.log("responseText: ")
9
console.log(xhr.responseText.toString())
10
var json = JSON.parse(xhr.responseText.toString())
11
console.log("json.status: ")
12
console.log(json.status)
13
}
14
}
15
}
16
​
17
xhr.open("POST", "https://hooks.zapier.com/hooks/catch/YYYYYY/ZZZZZZ/");
18
​
19
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
20
var data = "email="+encodeURIComponent("[email protected]") +
21
"&text="+encodeURIComponent("Message body text") +
22
"&subject="+encodeURIComponent("Email subject")
23
​
24
xhr.send(data)
Copied!

Mailgun

Send email with mailgun. Download the mailgun.pma example.
1. Open domain info in mailgun dashboard
2. Use the following code to send email. Replace it with your Domain and API Key:
1
function request() {
2
var xhr = new XMLHttpRequest();
3
xhr.onreadystatechange = function() {
4
​
5
if(xhr.readyState === XMLHttpRequest.DONE) {
6
console.log("Status code is: " + xhr.status + " (" + xhr.statusText + ")")
7
​
8
if (xhr.status == 200) {
9
console.log("responseText: ")
10
console.log(xhr.responseText.toString())
11
​
12
var json = JSON.parse(xhr.responseText.toString())
13
console.log("json.message: ")
14
console.log(json.message)
15
}
16
}
17
}
18
​
19
xhr.open("POST", "https://api.mailgun.net/v3/sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org/messages");
20
​
21
xhr.setRequestHeader('Authorization', "Basic " + Qt.btoa("api:key-YYYYYYYYYYYYYYYYYYYYYYYYY"));
22
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
23
​
24
var data = "from="+encodeURIComponent("[email protected]") +
25
"&to="+encodeURIComponent("[email protected]") +
26
"&subject="+encodeURIComponent("Subject message") +
27
"&text="+encodeURIComponent("Message body text")
28
​
29
xhr.send(data)
30
}
Copied!
Last modified 1yr ago
Copy link