# Networking

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:

```
function request() {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
            print('HEADERS_RECEIVED');
        } else if(xhr.readyState === XMLHttpRequest.DONE) {
            print('DONE');
        }
    }
    xhr.open("GET", "http://example.com");
    xhr.send();
}
```

Read more:

* [MDN web docs - Using XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest)
* [QML Book - HTTP Requests](https://qmlbook.github.io/ch11/index.html#http-requests)
* [QML Book - Rest Api](https://qmlbook.github.io/ch11/index.html#http-requests)

## Examples

### Zapier

Using [Zapier ](https://zapier.com)Webhooks. Download the [zapier.pma](https://s3-us-west-2.amazonaws.com/flipabit/examples/zapier.pma) example.

1\. Create new ZAP <https://zapier.com/apps/email/integrations/webhook/62/turn-webhooks-into-sent-emails>

2\. Add following fields

<div align="left"><img src="https://963067965-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIJW5nbmYRaSNWePAac%2F-MIxu2KP4MClY9gv_q2W%2F-MIxukSO-sYBdi1WowIz%2Fzapierinfields.png?alt=media&#x26;token=4bab0abf-d8a8-4c96-ba86-795eb5c5aac9" alt=""></div>

3\. Edit Template

<div align="left"><img src="https://963067965-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIJW5nbmYRaSNWePAac%2F-MIxu2KP4MClY9gv_q2W%2F-MIxukSNoBydHjkORjDd%2Fzapieremailtemplate.png?alt=media&#x26;token=f4052f4f-fd68-4fe7-aeca-95fdee883abd" alt=""></div>

4\. Turn on your ZAP

<div align="left"><img src="https://963067965-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIJW5nbmYRaSNWePAac%2F-MIxu2KP4MClY9gv_q2W%2F-MIxukSPV2JrA1oGC9f3%2Fzapierturnon.png?alt=media&#x26;token=b4b28e93-45a9-4b65-b069-86315f585395" alt=""></div>

5.Paste Webhook URL into code

<div align="left"><img src="https://963067965-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIJW5nbmYRaSNWePAac%2F-MIxu2KP4MClY9gv_q2W%2F-MIxukSQybWxr6NcdIoJ%2Fzapierurl.png?alt=media&#x26;token=48783ff6-b202-41fe-8767-f896de216931" alt=""></div>

The code:

```
var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {

    if(xhr.readyState === XMLHttpRequest.DONE) {
        console.log("Status code is: " + xhr.status + " (" + xhr.statusText + ")")
        if (xhr.status == 200) {
            console.log("responseText: ")
            console.log(xhr.responseText.toString())            
            var json = JSON.parse(xhr.responseText.toString())
            console.log("json.status: ")
            console.log(json.status)
        }
    }
}

xhr.open("POST", "https://hooks.zapier.com/hooks/catch/YYYYYY/ZZZZZZ/");

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var data =  "email="+encodeURIComponent("email@example.org") + 
            "&text="+encodeURIComponent("Message body text") + 
            "&subject="+encodeURIComponent("Email subject")

xhr.send(data)
```

### Mailgun

Send email with [mailgun](https://www.mailgun.com/). Download the [mailgun.pma](https://s3-us-west-2.amazonaws.com/flipabit/examples/mailgun.pma) example.

1\. Open domain info in mailgun dashboard

<div align="left"><img src="https://963067965-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIJW5nbmYRaSNWePAac%2F-MIxu2KP4MClY9gv_q2W%2F-MIxukR0arvotSSMlxL_%2Fmaildundashboard.png?alt=media&#x26;token=fd72b0e6-cc89-440d-9574-e92e7df9a55a" alt=""></div>

2\. Use the following code to send email. Replace it with your **Domain** and **API Key**:

```
function request() {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {        

        if(xhr.readyState === XMLHttpRequest.DONE) {
            console.log("Status code is: " + xhr.status + " (" + xhr.statusText + ")")

            if (xhr.status == 200) {
                console.log("responseText: ")
                console.log(xhr.responseText.toString())

                var json = JSON.parse(xhr.responseText.toString())
                console.log("json.message: ")
                console.log(json.message)
            }            
        }
    }

    xhr.open("POST", "https://api.mailgun.net/v3/sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org/messages");

    xhr.setRequestHeader('Authorization', "Basic " + Qt.btoa("api:key-YYYYYYYYYYYYYYYYYYYYYYYYY"));
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    var data =  "from="+encodeURIComponent("email@example.org") + 
                "&to="+encodeURIComponent("another@example.org") + 
                "&subject="+encodeURIComponent("Subject message") + 
                "&text="+encodeURIComponent("Message body text")

    xhr.send(data)
}
```

<div align="left"><img src="https://963067965-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIJW5nbmYRaSNWePAac%2F-MIxu2KP4MClY9gv_q2W%2F-MIxukR1Eawmlbo8hnaH%2Fmailgunconsole.png?alt=media&#x26;token=b4bf2921-83ba-4153-93d1-3cdbeee933de" alt=""></div>
