Testing with C#

For simplicity, all the C# examples will use the methods in this section

References and Namespace Imports

The methods shown in this page will require that System.Net.Http and Newtonsoft.Json to be included as a reference in your project.

Make sure you also include the following:

using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;

Help Methods

The following method sends a POST request to the specified URL and content using the .NET HttpClient class.

/// <summary>
/// Sends the POST Request.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="values">The values.</param>
/// <returns>The HTTP Content</returns>
public async Task<string> Post(string url, Dictionary<string,string> values)
{	
	using (var client = new HttpClient())
	{		
		SetBasicAuthentication(client);
		
		var json = JsonConvert.SerializeObject(values);
		HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
		
		var response = await client.PostAsync(url, content);	
		return await response.Content.ReadAsStringAsync();
	}	
}

The following method sends a GET request to the specified URL using the .NET HttpClient class.

/// <summary>
/// Sends the GET Request.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns>The HTTP Content</returns>
public async Task<string> Get(string url)
{
	using (var client = new HttpClient())
	{	
		SetBasicAuthentication(client);
	
		var result = await client.GetStringAsync(url);
		return result;
	}
}

The following method sends a DELETE request to the specified URL

/// <summary>
/// Sends the DELETE Request.
/// </summary>
/// <param name="url">The URL.</param>
/// <returns></returns>
public async Task<HttpResponseMessage> Delete(string url)
{
	using (var client = new HttpClient())
	{	
		SetBasicAuthentication(client);
		
		return await client.DeleteAsync(url);
	}
}

The following method sends a MERGE request to the specified URL in order to update entities

/// <summary>
/// Sends the MERGE Request.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="values">The values.</param>
/// <returns>The HTTP Content</returns>
public async Task<HttpResponseMessage> Update(string url, Dictionary<string,string> values)
{	
	var jObject = JObject.Parse(await Get(url));
	
	foreach(var value in values)
	{
		if(jObject[value.Key] != null )
		{
			jObject[value.Key] = value.Value;
		}
	}

	using (var client = new HttpClient())
	{	
		SetBasicAuthentication(client);
		var method = new HttpMethod("PATCH");
	
		var json = JsonConvert.SerializeObject(jObject);	
		HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");		
	
		var request = new HttpRequestMessage(method, url)
		{
			Content = content
		};
	
		return await client.SendAsync(request);		
	}
}

The following method handles Basic Authentication, you will need to change the credentials in the Authentication region

/// <summary>
/// Sets the basic authentication for the Http client.
/// </summary>
/// <param name="client">The client.</param>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
public void SetBasicAuthentication(HttpClient client)
{	
	var byteArray = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", new object[]{ Username, Password }));
	client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));	
}

#region Authentication
private const string Username = "username";
private const string Password = "password";
#endregion