when there is requirement to consume REST web service in Form-Data. BizTalk need to send request to REST Service in Form-Data format. And BizTalk always send data in JSON raw format by default. If we change the content type on send port still doesn’t work. So before sending file to REST Service, we need to convert file in form-Data.

Below is sample json file and corresponding Form-Data.

Sample JSON file

{

“value1”:22.24,

“value2”:70.780,

“description”:”test”,

“count”:1000

 }

Form-Data format will be:

Value1=22.24&value2=70.780&description=test&count=1000

We need to add custom pipeline in send pipeline Encode state after JSON Encoder component to convert JSON file in form-data.

Here we only provide execute function part in custom pipeline. For more details about how to create custom pipeline click here.

Code for Form-Data conversion:

public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)

        {

            IBaseMessagePart bodyPart = pInMsg.BodyPart;

            if (bodyPart != null)

            {               

                Stream originalStream = bodyPart.GetOriginalDataStream();

                string originalDataString;

                string postDataString = “”;

                try

                {

                    //fetch original message

                    Stream originalMessageStream = bodyPart.GetOriginalDataStream();

                    byte[] bufferOriginalMessage = new byte[originalMessageStream.Length];

                    originalMessageStream.Read(bufferOriginalMessage, 0, Convert.ToInt32(originalMessageStream.Length));

                    originalDataString = System.Text.ASCIIEncoding.ASCII.GetString(bufferOriginalMessage);

//add Newtonsoft.Json dll for below function from NuGet packege

                    var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(originalDataString);

                    StringBuilder sb = new StringBuilder();

                    foreach (KeyValuePair<string, string> kvp in dict)

                    {

                        if (!string.IsNullOrEmpty(kvp.Key) && !string.IsNullOrEmpty(kvp.Value))

                        {

                            if (sb.Length > 0) sb.Append(‘&’);

                            sb.Append(System.Web.HttpUtility.UrlEncode(kvp.Key));

                            sb.Append(‘=’);

                            sb.Append(System.Web.HttpUtility.UrlEncode(kvp.Value));

                        }

                    }

                     postDataString = sb.ToString();                                 

                }

                catch (Exception ex)

                {

                    ex.Source = “Form-Data Error”;

                    throw ex;

                }

                if (originalStream != null)

                {

                    byte[] outBytes = System.Text.Encoding.ASCII.GetBytes(postDataString);

                    MemoryStream memStream = new MemoryStream();

                    memStream.Write(outBytes, 0, outBytes.Length);

                    memStream.Position = 0;

                    bodyPart.Data = memStream;

                    pContext.ResourceTracker.AddResource(memStream);

                }

            }

            return pInMsg;

        }

Add content type at send port as below:

Content-Type:application/x-www-form-urlencoded

Hopefully this will help you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s