Click here to Skip to main content
13,404,806 members (37,944 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

2.2K views
Posted 2 Jan 2018

Uploading Documents to Yodal

, 2 Jan 2018
Rate this:
Please Sign up or sign in to vote.
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:

 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.

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()
{
    // Load the XML and get a string output
}

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; //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.

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)

Share

About the Author

Chamila Nishantha
Software Developer
Sri Lanka Sri Lanka
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180221.1 | Last Updated 3 Jan 2018
Article Copyright 2018 by Chamila Nishantha
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid