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.
![](https://uploads-us-west-2.insided.com/cdata-en/attachment/abd7a0bf-f627-49d9-b3ab-a31e4b671e6c.png)
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:
-
GET request to fetch the job details using the basic authentication.
-
GET request to fetch the job details using the auth token authentication.
-
POST request to execute the job using the basic authentication.
-
POST request to execute the job using the auth token authentication.
Please find the sample code below:
-
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}");
}
}
}
}
}
-
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}");
}
}
} }}
-
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(stringt] 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}");
}
}}}}
-
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(stringU] 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}");
}
}
}
}
}