Introduction
Yodal is a third party API where you can upload documents (XML, docx, etc.) for further processing. Below is the guideline for preparing the document for uploading. In order to communicate with Yodal, the following data is required:
Below is how to prepare the request:
- Prepare JSON body of the request
- Create a Hexadecimal MD5 of this string (Example: ”
5737142f561cb8355f98d64e3cc4637b
”) - Generate a time stamp in HTTP standard format (“
Fri, 06 May 2016 11:43:00 GMT
”) - Concatenate these 2
string
s (MD5+time stamp – > 5737142f561cb8355f98d64e3cc4637bFri, 06 May 2016 11:43:00 GMT
) - Generate SHA1 HMAC hexadecimal digest of this
string
using client secret
Request has to be configured as follows:
- Content-Type:
application/json
- Timestamp:
Fri, 06 May 2016 11:43:00 GMT
- Hash:
5737142f561cb8355f98d64e3cc4637b
- Authorization:
e6e3e276:17933485ca0e61d3d278fe1ec615bcf734b6649e
Wire up below code on a button click:
byte[] requestBody;
byte[] responseData;
string currentTimeStamp = string.Empty;
string jsonContent = string.Empty;
string calculatedHash = string.Empty;
string SHA1HMAC = string.Empty;
string authCode = string.Empty;
WebClient wClient = new WebClient();
jsonContent = CreateRequestBody();
requestBody = Encoding.UTF8.GetBytes(jsonContent);
currentTimeStamp = DateTime.Now.ToString("ddd, dd MMMM yyyy HH:mm:ss") + " " + "GMT";
calculatedHash = GetMD5HashCode(jsonContent);
SHA1HMAC = CreateAuthCode(calculatedHash + currentTimeStamp);
authCode = (clientIDYodal + ":" + SHA1HMAC);
wClient.Headers.Add("Timestamp", currentTimeStamp);
wClient.Headers.Add("Content-Type", "application/json");
wClient.Headers.Add("Hash", calculatedHash);
wClient.Headers.Add("Authorization", authCode);
responseData = wClient.UploadData(endPointForMatterYodal, "POST", requestBody);
string outPut;
outPut = wClient.Encoding.GetString(responseData);
dynamic dynObj = JsonConvert.DeserializeObject(outPut);
string matterURL = (string)dynObj["url"];
result = true;
Response.Redirect(matterURL, false);
Create the request body using the document which is supposed to send, below XML file is meant to be sent.
private string CreateRequestBody() {
string jsonBody = string.Empty;
jsonBody = ("{ \"xml\" " + " :" + "\"" + EncodeTo64(CreatJSONBodyFromXML()) + "\" + ""}");
return jsonBody;
}
Encode to base64String
:
private static string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
Get the string
of your document:
private string CreatJSONBodyFromXML()
{
}
Get MD5 hash code:
private string GetMD5HashCode(string input)
{
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
Create the authorization code:
private string CreateAuthCode(string input)
{
string message;
string key;
key = clientSecretYodal;
message = input;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(key);
HMACMD5 hmacmd5 = new HMACMD5(keyByte);
HMACSHA1 hmacsha1 = new HMACSHA1(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = hmacsha1.ComputeHash(messageBytes);
return (ByteToString(hashmessage));
}
endPointForMatterYodal
, clientIDYodal
, clientSecretYodal
variables carry the endpoint, client ID and the secret code respectively. Additionally, you may require the below references:
using Newtonsoft.Json;
using System.Security.Cryptography;
using System.Net;
When the code is executed, XML content will be submitted to Yodal environment. This can be code that can be configured for the user who has a Yodal account or not. If the user has a Yodal account ID, request should include the user id (userYodalID
) as well.
jsonBody = ("{ \"user\" " + ":" + userYodalID + ", \"xml\" :" +
"\"" + EncodeTo64(CreatJSONBodyFromXML()) + ""}")