Code Examples of how to connect to the AHDB RB209 Web API Application
Details
There are three different types of connections within the API, each are documented here in different programming languages.
The first three examples are coded in C#:
Those which have no parameters passed to them (Example 1),
those which have parameters passed to them within the uri (Example 2),
and those which have parameters passed to them within the body (Example 3).
The next three examples of connections are coded in AJAX:
Those which have no parameters passed to them (Example 4),
those which have parameters passed to them within the uri (Example 5),
and those which have parameters passed to them within the body (Example 6).
If you have registered for access to the API, and your request has been approved, you can connect to the connections within the API.
If not and you would like access to the API, then you can register from
here.
Once registered, you will need to acquire a token to authenticate your requests with.
Details on how to generate a token can be found here.
The token should be added to the connecting client (eg: Example 1: Line 7, Example 4: Lines 14 - 16).
The examples provided here, connect to the
/api/arable/cropGroups connection (Example 1: Line 10),
/api/arable/cropType/{cropGroupId} connection (Example 2: Line 13),
/api/Recommendation/recommendations connection (Example 3: Line 16),
/api/arable/cropTypes connection (Example 4: Line 10),
/api/arable/cropGroup/{cropGroupId} connection (Example 5: Line 10), and
/api/recommendation/recommendations connection (Example 6: Line 10)
of the current version of the API (Example 1: Line 3).
When parameters are passed in the uri (Example 2: Line 13), they are appended on to the end of the url - if there is more than 1 parameter, then they must be appended in the correct order.
However when parameters are passed in the body (Example 3: Lines 6, 9 & 17), the parameters first need to be serialized into a Json format, and then added to the connecting client.
The connections return data in a Json format which when the data is more than just a single string, must be deserialized (Example 1: Line 15) before it can be used in code.
In the case of Examples 1 and 2, these are Lists of type 'cropGroup' for Example 1, and type 'cropType' for Example 2.
Example 3 returns a data structure of type 'dataOutput'.
While Example 5 returns data as a string.
Please note that if you wish to connect to the Web API through AJAX, you will need to provide us with the url of the site(s) that you will be calling the API from.
Please email your details along with your User Name to:
nutrient.management@ahdb.org.uk,
with the subject line 'Web API - AJAX connection details' and we will update our system as soon as possible.
Example 1 C# (no parameters)
//API Connection Details
var accessToken = "..."; //Your access token
var apiConnection = "https://rb209.ahdb.org.uk";
//Add Authentication details
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
//Connect to API & retrieve data
var url = $"{apiConnection}/api/arable/cropGroups";
var task = client.GetAsync(url);
var returnedData = task.Result.Content.ReadAsStringAsync().Result;
//Deserialize output details
var cropGroupList = JsonConvert.DeserializeObject<List<CropGroup>>(returnedData);
Example 2 C# (parameters within uri)
//API Connection Details
var accessToken = "..."; //Your access token
var apiConnection = "https://rb209.ahdb.org.uk";
//Collect input parameter
var cropGroupId = int.Parse(ddlCropGroup.SelectedItem.Value);
//Add Authentication details
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
//Connect to API & retrieve data
var url = $"{apiConnection}/api/arable/cropType/{cropGroupId}";
var task = client.GetAsync(url);
var returnedData = task.Result.Content.ReadAsStringAsync().Result;
//Deserialize output details
var cropTypeList = JsonConvert.DeserializeObject<List<CropType>>(returnedData);
Example 3 C# (parameters within body)
//API Connection Details
var accessToken = "..."; //Your access token
var apiConnection = "https://rb209.ahdb.org.uk";
//Collect input parameters
var dataInput = ...; //Collection of input parameters required for endpoint
//Serialize input details
var content = new StringContent(JsonConvert.SerializeObject(dataInput), Encoding.UTF8, "application/json");
//Add Authentication details
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
//Connect to API & retrieve data
var url = $"{apiConnection}/api/Recommendation/recommendations";
var task = client.PostAsync(url, content);
var returnedData = task.Result.Content.ReadAsStringAsync().Result;
//Deserialize output details
var dataOutput = JsonConvert.DeserializeObject<DataOutput>(returnedData);
Example 4 AJAX (no parameters)
//API Connection Details
var accessToken = "..."; //Your access token
var apiConnection = "https://rb209.ahdb.org.uk";
//Clear dropdown list
$('#ddlCropType').empty();
$.ajax({
type: "GET",
url: apiConnection + "/api/arable/cropTypes/",
dataType: "json",
data: {},
//Pass in Authorization details
beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', "Bearer " + accessToken)
},
success: function (CropTypeItems) {
//Process output data: populate Crop Type dropdown list
$.each(CropTypeItems, function (i, cropType) {
$('#ddlCropType').append('<option value="' + cropType.cropTypeId + '">' + cropType.cropTypeName + '</option>');
});
},
error: function () {
alert('Failed to connect to API connection: CropType_List');
}
});
Example 5 AJAX (parameters within uri)
//API Connection Details
var accessToken = "..."; //Your access token
var apiConnection = "https://rb209.ahdb.org.uk";
//Collect input parameter
var cropGroupId = $("#txtCropGroupId").val();
$.ajax({
type: "GET",
url: apiConnection + "/api/arable/cropGroup/" + cropGroupId,
dataType: "json",
data: {},
//Pass in Authorization details
beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', "Bearer " + accessToken)
},
success: function(data){
//Output is a string
$('#lblCropGroup_Text').text(data.cropGroupName);
},
error: function () {
alert('Failed to connect to API connection: CropGroup_Item');
}
});
Example 6 AJAX (parameters within body)
//API Connection Details
var accessToken = "..."; //Your access token
var apiConnection = "https://rb209.ahdb.org.uk";
//Collect input parameters
var dataInput = ...; //Collection of input parameters required for endpoint
$.ajax({
type: "POST",
url: ApiConnection + "/api/recommendation/recommendations/",
dataType: "json",
data: dataInput,
//Pass in Authorization details
beforeSend: function (xhr){
xhr.setRequestHeader('Authorization', "Bearer " + accessToken)
},
success: function(returnedData){
//Process output data
var dataOutput = returnedData;
},
error: function () {
alert('Failed to connect to API connection: Recommendations');
}
});