- Home
- OpenAPI
- Sample Code
C#
//assigned Application Id
private const string appId = "4C782603-8B36-485A-AE33-557CF6392E44";
//authentication token - should be saved for up to an hour.
private static dynamic authToken = null;
//api base url
private const string apiBaseUrl = "https://openapitest.intelichart.com/";
//api relative urls
private const string vitalSignsUrl = "api/clinical/vitalsign/get";
static void Main(string[] args)
{
try
{
GetToken(); //note - this should be stored for up to an hour.
if (authToken == null)
{
Console.WriteLine("Could not get token");
return;
}
var json = @"
{
'personuid': '048114058233091195065092183127160152030014254207',
'startdate': '08/24/2015',
'enddate': '08/25/2015'
}";
try
{
var endResponse = Post(apiBaseUrl, json);
/* do something with response*/
/* check for null*/
}
catch (Exception ex1)
{
Console.WriteLine(ex.Message);
//handle error
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static dynamic Post(string url, string payload)
{
dynamic ret = null;
//convert to json object
var model = JsonConvert.DeserializeObject<dynamic>(payload) as object;
using (var client = new HttpClient(GetHttpClientHandler()))
{
//set the base address for api calls. This is based on environment you are calling
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
//set content type to json
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//this token gottent from GetToken() function earlier
//add authentication to header
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue(authToken.TokenType, authToken.Token.ToString());
//add clientId to header
client.DefaultRequestHeaders.Add("ApplicationId", appId);
//get response from server
using (var response = client.PostAsJsonAsync(vitalSignsUrl, model).Result)
{
var responseText = response.Content.ReadAsStringAsync().Result;
if (response.StatusCode != HttpStatusCode.OK) //error happened
{
Console.WriteLine(responseText);
}
else
{
//convert to json object
ret = JsonConvert.DeserializeObject<dynamic>(responseText);
//print out json in readable format
Console.WriteLine(JsonConvert.SerializeObject(ret, Formatting.Indented));
}
}
}
return ret;
}
private static HttpClientHandler GetHttpClientHandler()
{
var handler = new HttpClientHandler
{
UseCookies = false,
UseProxy = true,
UseDefaultCredentials = false
};
return handler;
}