This article will guide you through creating a user group in BioStar 2 server via BioStar 2 API.  


You can find a more detailed introduction to BioStar 2 New Local API in this article : [BioStar 2 API] How To Use BioStar 2 New Local API 

 

In this article, you can learn how to call an API function to create a user group in your BioStar 2 server. 

 

This article also includes a sample code of a C# program that incorporates the API, and also an example of API call made via Postman, a program used for building & testing RESTful API calls. 

 


[POST]          /user_groups



 [Parameters] 

ParameterTypeRequiredDescription
parent_id:idNumberYParent ID of the new User Group.
depthNumberNDepth of the User Group based on the parent.
nameStringYName of the User Group.



Postman Example 


Request Body Example:


Response Body Example:



C# Sample Code of API incorporated program   


static async void CreateUserGroup()

        {

            Console.WriteLine("*****CreateUserGroup Task Started*****");

            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(BioStarIP), new Cookie("bs-session-id", sessionID));

            string resourceAddress = BioStarIP + "/api/user_groups";

 

            JavaScriptSerializer serializer = new JavaScriptSerializer();

 

            Console.WriteLine("Enter User Group NAME: ");

            string name = Console.ReadLine();

            Console.WriteLine("Enter User Group DESCRIPTION: ");

            string description = Console.ReadLine();

            string payload = "{\"UserGroup\":{\"parent_id\":{\"id\":1},\"depth\":1,\"sync_device_groups\":[],\"sync_devices\":[],\"inherited\":true,\"text\":\"" + description + "\",\"name\":\"" + name + "\"}}";

               Console.WriteLine(payload);

            StringContent sc = new StringContent(payload, Encoding.UTF8, "application/json");

            HttpResponseMessage httpResponse = httpClient.PostAsync(resourceAddress, sc).Result;

 

            if (httpResponse.IsSuccessStatusCode == true)

            {

                Console.WriteLine("Create User Group Successful.");

                string httpResponseBody = await httpResponse.Content.ReadAsStringAsync();

                   Console.WriteLine(httpResponseBody);

               }

            else

            {

                Console.WriteLine("Create User Group Failed.");

                   Console.WriteLine(httpResponse.ToString());

            }

        }

 

When you run the above program to create user group, you'll see something similar to below screenshot : 

(Please input 'User Group Name' and 'User Group Name Description' accordingly  when prompted as below. )


텍스트이(가) 표시된 사진

자동 생성된 설명