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
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));curl -X POST \
https://r-sock.com/api/fetch/users \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-d '{"id": 3}'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);
});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);
}/fetch users {"id" : 3}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));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 }'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);
});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);
}
/create users { "name": "Tom", "age": 27, "point": 100 }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));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} }'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);
});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);
}/craete users { "where": {"id": 3}, "data": {"age": 23, "point": 200 } }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));curl -X POST \
https://r-sock.com/api/delete/users \
-H "Authorization: Bearer your_token" \
-H "Content-Type: application/json" \
-d '{"id": 3}'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);
});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);
}/delete users { "id": 3 }Result
{"code": 200 }Last updated