> For the complete documentation index, see [llms.txt](https://docs.flipabit.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flipabit.dev/networking.md).

# 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="/files/-MIxukSO-sYBdi1WowIz" alt=""></div>

3\. Edit Template

<div align="left"><img src="/files/-MIxukSNoBydHjkORjDd" alt=""></div>

4\. Turn on your ZAP

<div align="left"><img src="/files/-MIxukSPV2JrA1oGC9f3" alt=""></div>

5.Paste Webhook URL into code

<div align="left"><img src="/files/-MIxukSQybWxr6NcdIoJ" 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="/files/-MIxukR0arvotSSMlxL_" 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="/files/-MIxukR1Eawmlbo8hnaH" alt=""></div>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flipabit.dev/networking.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
