Table of Contents |
---|
When writing clients in C# to consume the API, You can use the ServiceStack client (easy way) or use the .NET web client (harder way).
Using the ServiceStack Client
- Install the ServiceStackVS extension
- Add the nuget package ServiceStack.Client
- Add References to System.Runtime.Serialization and System.Net
- Right-click on the project and choose "Add ServiceStack Reference..."
- Enter the address of the host and name the service
Add the following Code:
Code Block language c# title Using ServiceStack.Client // Authenticate var client = new ServiceStack.JsonServiceClient("http://api.jiwa.com.au"); var authResponse = client.Send<ServiceStack.AuthenticateResponse>(new ServiceStack.Authenticate() { provider = "credentials", UserName = "api", Password = "password", RememberMe = true }); // Read a debtor var serviceStackDebtorResponse = client.Get(new DebtorGetRequest { DebtorID = "0000000061000000001V" });
Using the .NET WebClient
- Add References to System.Runtime.Serialization and System.Net
- Add the nuget package Newtonsoft.Json
- Add a new class file to the project and paste the code from the code generator of the API - you can get this by clicking on the "Generate C#" link on the metadata page:
Create a cookie aware WebClient using this code - this is needed to send the session Id with every request:
Code Block language c# title Cookie Aware WebClient public class CookieAwareWebClient : WebClient { public CookieAwareWebClient() { CookieContainer = new CookieContainer(); } public CookieContainer CookieContainer { get; private set; } protected override WebRequest GetWebRequest(Uri address) { var request = (HttpWebRequest)base.GetWebRequest(address); request.CookieContainer = CookieContainer; return request; } }
Add the following code:
Code Block language c# title Using WebClient using (var webclient = new CookieAwareWebClient()) { // Authenticate webClient.QueryString.Add("username", "api"); webClient.QueryString.Add("password", "password"); string responsebody = webclient.DownloadString("http://api.jiwa.com.au/auth"); ServiceStack.AuthenticateResponse webClientAuthResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ServiceStack.AuthenticateResponse>(responsebody); // Read a debtor responsebody = webclient.DownloadString("http://api.jiwa.com.au:80/debtor/0000000061000000001V"); JiwaFinancials.Jiwa.JiwaServiceModel.DebtorGetResponse webClientDebtorGetResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<JiwaFinancials.Jiwa.JiwaServiceModel.DebtorGetResponse>(responsebody); }
Dependency-Free
You can, of course, also consume the REST API without any strong typing or reference to our DTO classes.
Below is a code sample showing how to consume the REST API to logon and retrieve a debtor record with nothing more than a reference to the Newtonsoft.Json nuget package for deserialisation. This sample also uses the .NET WebClient, but manually sets the SessionId cookie.
Code Block | ||||
---|---|---|---|---|
| ||||
using (var webClient = new System.Net.WebClient()) { // Authenticate webClient.QueryString.Add("username", "api"); webClient.QueryString.Add("password", "password"); string responsebody = webClient.DownloadString("http://localhost/auth"); // Above returns something like this: {"SessionId":"0hKBFAnutUk8Mw6YY6DN","UserName":"api","DisplayName":"","ResponseStatus":{}} // Deserialise response into a dynamic - below requires the Newtonsoft.Json nuget package var authResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(responsebody); var sessionId = authResponse.SessionId; webClient.Headers.Add(System.Net.HttpRequestHeader.Cookie, string.Format("ss-id={0}", sessionId)); responsebody = webClient.DownloadString("http://localhost/Debtors/0000000061000000001V"); } |
Related articles
Filter by label (Content by label) | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Page Properties | ||
---|---|---|
| ||
|