> For the complete documentation index, see [llms.txt](https://bt-computing-1.gitbook.io/restsocket/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bt-computing-1.gitbook.io/restsocket/getting-started/openapi.md).

# Basic Usage

How to Use the REST API

The Restsocket Server enables easy interaction with your database via a RESTful API. After configuring the connection string and starting the server, it connects to your database and provides endpoints for fetching, creating, updating, and deleting data.  &#x20;

### Basic Usage

In Restsocket, all calls use POST requests. The distinction between the user and the server is made through the Bearer Token. The given Bearer Token must be sent in the Authorization header.

```javascript
fetch('https://r-sock.com/api/fetch/users', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your_token',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ id: 3 })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
```

Replace Bearer  your\_token  with your  actual Bearer token.

## Example Data

The examples below assume that there is a 'users' table and data as follows.

### Table : users

| id (bigint) | name (varchar2) | age (int) | point (int) |
| ----------- | --------------- | --------- | ----------- |
| 3           | Tom             | 27        | 100         |

## Fetch Operation

Use the POST call as follows.&#x20;

### End-point

```html
https://r-sock.com/api/fetch/<table_name>
```

### Post Data

> { column1 :  value1, ... }

### Example

{% tabs %}
{% tab title="JavaScript" %}

```javascript
fetch('https://r-sock.com/api/fetch/users', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your_token',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ id: 3 })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl -X POST \
https://r-sock.com/api/fetch/users \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-d '{"id": 3}'
```

{% endtab %}

{% tab title="Axios" %}

```ruby
axios.post('https://r-sock.com/api/fetch/users', {
  headers: {
    'Authorization': 'Bearer your_token',
  },
  params: {
    id: 3,
  }
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error('There was an error!', error);
});
```

{% endtab %}

{% tab title="Unity" %}

```csharp
WWWForm wwwForm = new WWWForm();
wwwForm.AddField("id", "3");

UnityWebRequest www = UnityWebRequest.Post("https://r-sock.com/api/fetch/users", wwwForm);
www.SetRequestHeader("Authorization", "Bearer your_token");
yield return www.SendWebRequest();

if (www.result == UnityWebRequest.Result.Success) {
    Debug.Log(www.downloadHandler.text);
} else {
    Debug.LogError("Error: " + www.error);
}
```

{% endtab %}

{% tab title="Slack App" %}

```bash
/fetch users {"id" : 3}
```

{% endtab %}
{% endtabs %}

### Result

```json
{"code": 200, 
  "data": [
    {"id": 3, "name": "Tom", "age": 27, "point" : 100}
  ]
}
```

## Create Operation

Use the POST call as follows.&#x20;

### End-point

```html
https://r-sock.com/api/create/<table_name>
```

### Post Data

> &#x20;{ column1 :  value1, column2 : value2, ... } &#x20;

### Example

{% tabs %}
{% tab title="JavaScript" %}

```javascript
fetch('https://r-sock.com/api/create/users', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your_token',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ name: 'Tom', age: 27, point: 100 })
})
.then(response => response.json())
.then(data => console.log('Success', data))
.catch(error => console.error('Error:', error));
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl -X POST \
https://r-sock.com/api/create/users \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-d '{ "name": 'Tom", "age": 27, "point": 100 }'
```

{% endtab %}

{% tab title="Axios" %}

```ruby
axios.post('https://r-sock.com/api/fetch/users', {
  headers: {
    'Authorization': 'Bearer your_token',
  },
  params: {
    name: 'Tom',
    age: 27,
    point: 100
  }
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error('There was an error!', error);
});
```

{% endtab %}

{% tab title="Unity" %}

```csharp
WWWForm wwwForm = new WWWForm();
wwwForm.AddField("name", "Tom");
wwwForm.AddField("age", "27");
wwwForm.AddField("point", "100");

UnityWebRequest www = UnityWebRequest.Post("https://r-sock.com/api/fetch/users", wwwForm);
www.SetRequestHeader("Authorization", "Bearer your_token");
yield return www.SendWebRequest();

if (www.result == UnityWebRequest.Result.Success) {
    Debug.Log(www.downloadHandler.text);
} else {
    Debug.LogError("Error: " + www.error);
}



```

{% endtab %}

{% tab title="Slack App" %}

```bash
/create users { "name": "Tom", "age": 27, "point": 100 }
```

{% endtab %}
{% endtabs %}

### Result

```json
{"code": 200,"new_id": 3}
```

## Update  Operation

Use the POST call as follows.&#x20;

### End-point

```html
https://r-sock.com/api/update/<table_name>
```

### Post Data

> { "where" : { column1 :  value1, ... },&#x20;
>
> &#x20; "data" : { column2 : value2, ... } }&#x20;

### Example

{% tabs %}
{% tab title="JavaScript" %}

```javascript
fetch('https://r-sock.com/api/update/users', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your_token',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify( {
    where: { id: 3 },
    data: { age: 28, point: 200 } } )
})
.then(response => response.json())
.then(data => console.log('Success', data))
.catch(error => console.error('Error:', error));
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl -X POST \
https://r-sock.com/api/update/users \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-d '{ "where": {"id": 3}, "data": {"age": 23, "point": 200} }'
```

{% endtab %}

{% tab title="Axios" %}

```ruby
axios.post('https://r-sock.com/api/fetch/users', {
  headers: {
    'Authorization': 'Bearer your_token',
  },
  params: {
    where: { id: 3 },
    data: { age: 28, point: 200 }    
  }
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error('There was an error!', error);
});
```

{% endtab %}

{% tab title="Unity" %}

```csharp
string url = "https://r-sock.com/api/update/users";
string jsonPayload 
  = "{\"where\": { \"id\": 3 }, \"data\": { \"age\": 28, \"point\": 200 }}";

UnityWebRequest request = new UnityWebRequest(url, "POST");

request.SetRequestHeader("Authorization", "Bearer your_token");
request.SetRequestHeader("Content-Type", "application/json");

byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonPayload);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);

request.downloadHandler = new DownloadHandlerBuffer();

yield return request.SendWebRequest();

if (request.result == UnityWebRequest.Result.ConnectionError 
    || request.result == UnityWebRequest.Result.ProtocolError) {
    Debug.LogError("Error: " + request.error);
}
else {
    Debug.Log("Success: " + request.downloadHandler.text);
}
```

{% endtab %}

{% tab title="Slack App" %}

```bash
/craete users { "where": {"id": 3}, "data": {"age": 23, "point": 200 } }
```

{% endtab %}
{% endtabs %}

### Result

```json
{"code": 200 }
```

## Delete Operation

Use the POST call as follows.&#x20;

### End-point

```html
https://r-sock.com/api/delete/<table_name>
```

### Post Data

> &#x20;{ column1 :  value1, ... }&#x20;

### Example

{% tabs %}
{% tab title="JavaScript" %}

```javascript
fetch('https://r-sock.com/api/delete/users', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer your_token',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ id: 3 })
})
.then(response => response.json())
.then(data => console.log('Success', data))
.catch(error => console.error('Error:', error));
```

{% endtab %}

{% tab title="CURL" %}

```bash
curl -X POST \
https://r-sock.com/api/delete/users \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-d '{"id": 3}'
```

{% endtab %}

{% tab title="Axios" %}

```ruby
axios.post('https://r-sock.com/api/delete/users', {
  headers: {
    'Authorization': 'Bearer your_token',
  },
  params: {
    id: 3,
  }
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error('There was an error!', error);
});
```

{% endtab %}

{% tab title="Unity" %}

```csharp
WWWForm wwwForm = new WWWForm();
wwwForm.AddField("id", "3");

UnityWebRequest www 
    = UnityWebRequest.Post("https://r-sock.com/api/delete/users", wwwForm);
    
www.SetRequestHeader("Authorization", "Bearer your_token");
yield return www.SendWebRequest();

if (www.result == UnityWebRequest.Result.Success) {
    Debug.Log(www.downloadHandler.text);
} else {
    Debug.LogError("Error: " + www.error);
}
```

{% endtab %}

{% tab title="Slack App" %}

```bash
/delete users { "id": 3 }
```

{% endtab %}
{% endtabs %}

### Result

```json
{"code": 200 }
```
