# Requests

A request is a JSON structure that is passed via a POST HTTP call to ws4sqlite, using the port specified when [running](https://germ.gitbook.io/ws4sqlite/v0.15/running#port) the server application.

First and foremost, the database we connect to is specified in the URL of the POST call. It is something like this:

```bash
http://localhost:12321/db2
```

That `db2` part is the database ID, and must match the `id` of a database, defined in the [commandline](https://germ.gitbook.io/ws4sqlite/v0.15/running#databases-and-config-companion-files).

This is a JSON that exemplifies all possible elements of a request (but for [encryption](https://germ.gitbook.io/ws4sqlite/v0.15/documentation/encryption), to keep things simple).

```json
{
    "credentials": {
        "user": "myUser1",
        "password": "myCoolPassword"
    },
    "transaction": [
        {
            "query": "SELECT * FROM TEMP"
        },
        {
            "query": "SELECT * FROM TEMP WHERE ID = :id",
            "values": { "id": 1 }
        },
        {
            "statement": "INSERT INTO TEMP (ID, VAL) VALUES (0, 'ZERO')"
        },
        {
            "noFail": true,
            "statement": "INSERT INTO TEMP (ID, VAL) VALUES (:id, :val)",
            "values": { "id": 1, "val": "a" }
        },
        {
            "statement": "#Q2",
            "valuesBatch": [
                { "id": 2, "val": "b" },
                { "id": 3, "val": "c" }
            ]
        }
    ]
}
```

Let's go through it.

#### Authentication Credentials

*Lines 2-5; object*

If [authentication](https://germ.gitbook.io/ws4sqlite/v0.15/documentation/authentication) is enabled *in `INLINE` mode*, this object describes the credentials. See the [detailed docs](https://germ.gitbook.io/ws4sqlite/v0.15/authentication#credentials-in-the-request-inline-mode).

#### List of Queries/Statements

*Line 6; list of objects; mandatory*

**Must be not empty**. The list of the queries or statements that will actually be performed on the database, with their own parameters.

They will be run in a transaction, and the transaction will be committed only if all the queries that are *not* marked as `noFail` (see the [relevant section](https://germ.gitbook.io/ws4sqlite/v0.15/documentation/errors)) do successfully complete.

#### SQL Statements to Execute

*Lines 8, 11, 15, 19; string; mandatory one of `query` or `statement`*

The actual SQL to execute.

Specifying it as `query` means that a result set is expected (typically, `SELECT` queries or queries with a `RETURNING` clause).

Specifying a `statement` will not return a result set, but a count of affected records.

#### Stored Query Reference

*Line 23; string; mandatory as the above*

A `query` or a `statement` (see above) can consist of a reference to a Stored Query. They are prepended by a `#`. An error will occour if the S.Q. with an ID equal to the part after the `#` was not defined for this database.

See the [relevant section](https://germ.gitbook.io/ws4sqlite/v0.15/documentation/stored-statements).

#### Parameter Values for the Query/Statement

*Lines 12, 20; object*

If the query needs to be parametrized, named parameters can be defined in the statement using SQLite [syntax](https://www.sqlite.org/c3ref/bind_blob.html)(e.g. `:id` or `@id`), and the proper values for them must be specified here. You can specify values that do not match a parameter; they'll be ignored.

{% hint style="warning" %}
What happens if some parameter values aren't defined in the `values` object? If there are *less* parameter values than expected, it will give an error. If they are correct in number, but some parameter names don't match, the missing parameters will be assigned a value of `null`.
{% endhint %}

#### Batch Parameter Values for a Statement

*Lines 24..27; list of objects*

Only for `statement`s, more than one set of parameter values can be specified; the statement will be applied to them in a batch (by *preparing* the statement).

#### NoFail: Don't Fail when Errors Occour

*Line 18; Boolean*

When a query/statement fails, by default the whole transaction is rolled back and a response with a single error is returned (the first one for the whole transaction). Specifying this flag, the error will be reported for that statement, but the execution will continue and eventually be committed. See the [relevant page](https://germ.gitbook.io/ws4sqlite/v0.15/documentation/errors) for more details.
