All NHVR APIs use the REST API (Representational state transfer) an API that uses HTTP requests for communication with web services.
To more easily use JSON data in C#, you will need to install the JSON.Net library from https://www.newtonsoft.com/json.
All API calls require the endpoint URL and a set of headers. Some API calls will also require parameters (GET or DELETE requests) or data (POST or PUT) requests.
Login API calls should include the following headers:
Header | Value | Note |
Content-Type | 'application/json' | All calls are made using JSON data structures |
Ocp-Apim-Subscription-Key | Your subscription key for the NHVR Portal API | The subscription key uniquely identifies you to the NHVR Portal API |
The login endpoint uses the POST method and because the subscription key automatically populates your email and password the data body should contain an empty object.
using System;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Net.Http;
using System.Web;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace CSHttpClientSample
{
public class LoginResponse
{
public string token { get; set; }
public string refreshToken { get; set; }
}
static class Program
{
static void Main()
{
MainAsync();
Console.ReadLine();
}
static async void MainAsync()
{
var client = new HttpClient();
var subscriptionKey = "<subscription key>";
var jwt = await GetJWTFromLogin(client, subscriptionKey);
Console.WriteLine(jwt);
}
static async Task<string> GetJWTFromLogin(HttpClient client, string subscriptionKey)
{
var uri = "https://api-public.nhvr.gov.au/auth/users/login";
// Headers
client.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Request body
// If you have multiple accounts
// use accountType to select the type of account and
// use accountId to select the account id
var body = new {};
// Post
HttpResponseMessage response = await client.PostAsJsonAsync(uri, body);
string jsonResponse = response.Content.ReadAsStringAsync().Result;
LoginResponse loginResponseObj = JsonConvert.DeserializeObject<LoginResponse>(jsonResponse);
return loginResponseObj.token;
}
}
}