Introduction
If you’d like to use BioStar 2 New Local API as a RESTful API for your own customization or other purposes, you can simply reference this article. This article will cover the most basic usage of the BioStar 2 New Local API by C# console application.
This is just a sample application made for those who might need to use the BioStar 2 New Local API. It is a Visual C# console application.
Features
This article will cover how to register/unregister user profile photo via New Local API.
Through this article, you can learn how to call an API function to register profile photos to the users in BioStar 2 server. You can use [PUT] /api/users/{id} function, which is for updating any information of a user, to register/unregister user profile photos.
For most of the image-related input, you must convert images to Base64 format. Please, take note that you must not have “Base64 :” in your input for “photo”.
If you have the “photo” value as empty, it will unregister the current user profile photo.
"photo": "/9j/4AAQSkZJRgABAQEASABI…” | Good |
"photo": "Base64: /9j/4AAQSkZJRgABAQEASABI…” | Bad |
“photo”: “” | Unregister profile photo |
Make sure you take a good look at the example code of the C# program built for API calls. You can simply copy & paste the source code to use the same function in your own integration.
You can also see how to call the API functions via Postman, which is a program used for RESTful API calls.
Part 1. API Call & Parameters
[PUT]: /users/{id}
[Parameters]
Group | Name | Type | *M/O | Explanation | Value |
User | photo | string | O | Base64 format |
|
* M – Mandatory, O – Optional
Part 2. Request Body & Response Model
[Example Value/Parameters Model]
{ "User": { "photo ": "/9j/4AAQSKZJRgABAQEA…" } } |
[Response Model]
{ "DeviceResponse": { "rows": [ { "id": "541530887", "code": "65230" } ], "result": "false" }, "Response": { "code": "0", "link": "https://support.supremainc.com/en/support/home", "message": "Success" } } |
[Response: Fail]
{ "Response": { "code": "65675", "link": "https://support.supremainc.com/en/support/home", "message": "not defined" } } |
[Response: Successful]
{ "Response": { "code": "0", "link": "https://support.supremainc.com/en/support/home", "message": "Success" } } |
Part 3. Console User Profile Photo Registration Example
[Register User Profile Photo Method Source Code]
The source code has a function to convert the image path to the ‘Base64’ format. You just have to enter the image’s {path\imagename.format} in order to register user profile photo.
static async void RegisterProfilePhoto() { if (sessionID == null) { Console.WriteLine("You must log in first!"); return; } CookieContainer cookieContainer = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookieContainer;
HttpClient httpClient = new HttpClient(handler);
HttpClient client = new HttpClient(handler); httpClient.DefaultRequestHeaders.Add("bs-session-id", sessionID); cookieContainer.Add(new Uri("https://127.0.0.1"), new Cookie("bs-session-id", sessionID)); ListUsers(); Console.WriteLine("Enter USER ID to register profile photo: "); string userid = Console.ReadLine(); string resourceAddress = "https://127.0.0.1/api/users/" + userid + ""; string photopath = ""; String file = ""; JavaScriptSerializer serializer = new JavaScriptSerializer(); Console.WriteLine("Enter photo path: *Leave EMPTY to UNREGISTER Current Profile Photo"); photopath = Console.ReadLine(); if(photopath != "") { Byte[] bytes = File.ReadAllBytes(photopath); file = Convert.ToBase64String(bytes); }
string payload2 = "{ \"User\": { "; payload2 = payload2 + "\"photo\": \"" + file + "\"}}"; Console.WriteLine(payload2); StringContent sc = new StringContent(payload2, Encoding.UTF8, "application/json"); //HttpResponseMessage httpResponse = await httpClient.PutAsync(resourceAddress, sc); HttpResponseMessage httpResponse = httpClient.PutAsync(resourceAddress, sc).Result;
if (httpResponse.IsSuccessStatusCode == true) { if (photopath != "") { Console.WriteLine("Profile Photo has been registered."); string httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); Console.WriteLine(httpResponseBody); } else{ Console.WriteLine("Profile Photo has been unregistered."); string httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); Console.WriteLine(httpResponseBody); } } else { Console.WriteLine("Failed to register Profile Photo."); Console.WriteLine(httpResponse.ToString()); } } |
[Enter the appropriate input]
Input: image’s path\image file name
[After registering Successfully]
Part 4. Register User Profile Photo via Postman
[Request Example]
[Response Example: body]