How to run the Sync API endpoints using C# code


Userlevel 2
Badge

This post will demonstrate how to call the Sync API using C# code. We can run the API endpoint using 2 authentication methods: 

 
1. Basic authentication: Using your username and password from Sync. 
2. AuthToken: The authorization token which can be fetched from the application by navigating to Settings >> Users >> Choose the username and click on Edit. 
 

 
 

The following examples will show how run GET requests to list the jobs and POST requests to execute a job for both authentication types, specifically: 

  1. GET request to fetch the job details using the basic authentication. 

  1. GET request to fetch the job details using the auth token authentication. 

  1. POST request to execute the job using the basic authentication. 

  1. POST request to execute the job using the auth token authentication. 

 

Please find the sample code below: 

  1. GET request to fetch the job details using the basic authentication: 

using System; 

using System.Net.Http; 

using System.Text; 

using System.Threading.Tasks; 

 

namespace httpClient 

    class Program 

    { 

        static async Task Main(string[] args) 

        { 

            // Username and password for basic authentication 

            string username = "<Your password>"; 

            string password = "<Your username>"; 

 

            // Concatenate username and password separated by colon 

            string credentials = $"username:password"; 

 

            // Encode credentials in Base64 

            string credentialsBase64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(credentials)); 

 

            // URL to which you are making the GET request 

            string url = "http://localhost:8181/api.rsc/jobs/"; 

 

            // Create an HttpClient instance 

            using (HttpClient client = new HttpClient()) 

            { 

                // Add authorization header with Basic scheme and encoded credentials 

                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentialsBase64); 

 

                // Send the GET request 

                HttpResponseMessage response = await client.GetAsync(url); 

 

                // Check if request was successful 

                if (response.IsSuccessStatusCode) 

                { 

                    // Read the response content 

                    string responseBody = await response.Content.ReadAsStringAsync(); 

                    Console.WriteLine(responseBody); 

                } 

                else 

                { 

                    Console.WriteLine($"Failed to get data. Status code: {response.StatusCode}"); 

                } 

            } 

        } 

    } 


 

  1. GET request to fetch the job details using the auth token authentication: 

using System; 

using System.Net.Http; 

using System.Threading.Tasks; 

  

namespace httpClient 

    class Program 

    { 

        static async Task Main(string[] args) 

        { 

            string accessToken = "<Your auth token>"; 

  

            // URL to which you are making the GET request 

            string url = "http://localhost:8181/api.rsc/jobs/"; 

  

            // Create an HttpClient instance 

            using (HttpClient client = new HttpClient()) 

            { 

                // Add authorization header 

                client.DefaultRequestHeaders.Add("x-cdata-authtoken", accessToken); 

  

                // Send the GET request 

                HttpResponseMessage response = await client.GetAsync(url); 

  

                // Check if request was successful 

                if (response.IsSuccessStatusCode) 

                { 

                    // Read the response content 

                    string responseBody = await response.Content.ReadAsStringAsync(); 

                    Console.WriteLine(responseBody); 

                } 

                else 

                { 

                    Console.WriteLine($"Failed to get data. Status code: {response.StatusCode}"); 

                } 

            } 

        } }} 

 

  1. POST request to execute the job using the basic authentication: 

using System; 

using System.Net.Http; 

using System.Text; 

using System.Threading.Tasks; 

 

namespace httpClient 

    class Program 

    { 

        static async Task Main(string[] args) 

        { 

            // Username and password for basic authentication 

            string username = "<Your username>"; 

            string password = "<Your password>"; 

 

            // Concatenate username and password separated by colon 

            string credentials = $"username:password"; 

 

            // Encode credentials in Base64 

            string credentialsBase64 = Convert.ToBase64String(Encoding.ASCII.GetBytes(credentials)); 

 

            // URL to which you are making the POST request 

            string url = "http://localhost:8181/api.rsc/executeJob/"; 

 

            // Data to be sent in the request body (if any) 

            string postData = "{\"JobId\": \"value\", \"JobName\": \"value\"}"; 

 

            // Create an HttpClient instance 

            using (HttpClient client = new HttpClient()) 

            { 

                // Add authorization header with Basic scheme and encoded credentials 

                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentialsBase64); 

 

                // Create StringContent with the data to be sent in the request body 

                var content = new StringContent(postData, Encoding.UTF8, "application/json"); 

 

                // Send the POST request 

                HttpResponseMessage response = await client.PostAsync(url, content); 

 

                // Check if request was successful 

                if (response.IsSuccessStatusCode) 

                { 

                    // Read the response content 

                    string responseBody = await response.Content.ReadAsStringAsync(); 

                    Console.WriteLine(responseBody); 

                } 

                else 

                { 

                    Console.WriteLine($"Failed to get data. Status code: {response.StatusCode}"); 

                } 

            }}}} 

 

  1. POST request to execute the job using the auth token authentication: 

using System; 

using System.Net.Http; 

using System.Text; 

using System.Threading.Tasks; 

 

namespace httpClient 

    class Program 

    { 

        static async Task Main(string[] args) 

        { 

            string accessToken = "<Your auth token>"; 

 

            // URL to which you are making the POST request 

            string url = "http://localhost:8181/api.rsc/executeJob/"; 

 

            // Data to be sent in the request body (if any) 

            string postData = "{\"JobId\": \"value\", \"JobName\": \"value\"}"; 

 

            // Create an HttpClient instance 

            using (HttpClient client = new HttpClient()) 

            { 

                // Add authorization header 

client.DefaultRequestHeaders.Add("x-cdata-authtoken", accessToken); 

 

                // Create StringContent with the data to be sent in the request body 

                var content = new StringContent(postData, Encoding.UTF8, "application/json"); 

 

                // Send the POST request 

                HttpResponseMessage response = await client.PostAsync(url, content); 

 

                // Check if request was successful 

                if (response.IsSuccessStatusCode) 

                { 

                    // Read the response content 

                    string responseBody = await response.Content.ReadAsStringAsync(); 

                    Console.WriteLine(responseBody); 

                } 

                else 

                { 

                    Console.WriteLine($"Failed to get data. Status code: {response.StatusCode}"); 

                } 

            } 

        } 

    } 


This topic has been closed for comments