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.

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.

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.

End-point

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

Post Data

{ column1 : value1, ... }

Example

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));

Result

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

Create Operation

Use the POST call as follows.

End-point

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

Post Data

{ column1 : value1, column2 : value2, ... }

Example

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));

Result

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

Update Operation

Use the POST call as follows.

End-point

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

Post Data

{ "where" : { column1 : value1, ... },

"data" : { column2 : value2, ... } }

Example

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));

Result

{"code": 200 }

Delete Operation

Use the POST call as follows.

End-point

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

Post Data

{ column1 : value1, ... }

Example

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));

Result

{"code": 200 }

Last updated