BizTalk with Rest Service Form-Data

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







Form-Data format will be:


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 = “”;



                    //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(‘&’);






                     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;




            return pInMsg;


Add content type at send port as below:


Hopefully this will help you.

Leave a Reply

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

You are commenting using your 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