Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET
Tip/Trick

Uploading Documents to Yodal

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Jan 2018CPOL1 min read 6K  
Simple way to upload document to Yodal

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 strings (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:

JavaScript
 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";
 //string currentTimeStamp = "Fri, 06 May 2016 11:43:00 GMT";

calculatedHash = GetMD5HashCode(jsonContent);
//string calculatedHash = "5737142f561cb8355f98d64e3cc4637b";

SHA1HMAC = CreateAuthCode(calculatedHash + currentTimeStamp);

//authCode = "e6e3e276:17933485ca0e61d3d278fe1ec615bcf734b6649e";
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.

JavaScript
private string CreateRequestBody() {
    string jsonBody = string.Empty;
    jsonBody = ("{ \"xml\" " + " :" + "\"" + EncodeTo64(CreatJSONBodyFromXML()) + "\" + ""}");
    return jsonBody;
}

Encode to base64String:

JavaScript
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:

JavaScript
private string CreatJSONBodyFromXML()
{
    // Load the XML and get a string output
}

Get MD5 hash code:

JavaScript
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:

JavaScript
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:

JavaScript
using Newtonsoft.Json; //Newtonsoft library (https://www.nuget.org/packages/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.

JavaScript
jsonBody = ("{ \"user\" " + ":" + userYodalID + ", \"xml\" :" + 
            "\"" + EncodeTo64(CreatJSONBodyFromXML()) + ""}")

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --