After our first attempt at solving this we decided to go with Greg Forsythe's code. The C# code in its entirety is down below. I am also showing the request from the Postman (which works perfectly) and the one from BizTalk (which doesnt work). But as you can see we are very very close at solving this one. I must also mention that we do not have any mime component any more in the pipeline.
public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pContext, Microsoft.BizTalk.Message.Interop.IBaseMessage pInMsg)
{
return CreateMultipartMIME(pContext, pInMsg);
}
private IBaseMessage CreateMultipartMIME(IPipelineContext pContext, IBaseMessage pInMsg)
{
// Boundary
string separator = Guid.NewGuid().ToString();
// New message
IBaseMessage outMsg = pContext.GetMessageFactory().CreateMessage();
outMsg.Context = PipelineUtil.CloneMessageContext(pInMsg.Context);
// Create messagepart (BodyPart)
IBaseMessagePart multiPart = pContext.GetMessageFactory().CreateMessagePart();
multiPart.ContentType = "text/plain";
multiPart.Data = CreateMIME(pInMsg, separator);
// Add messagepart as BodyPart
outMsg.AddPart("multiPart", multiPart, true);
outMsg.BodyPart.ContentType = string.Format("multipart/form-data; boundary={0}", separator);
outMsg.Context.Promote("ContentType", "http://schemas.microsoft.com/BizTalk/2003/http-properties", string.Format("multipart/form-data; boundary={0}", separator));
return outMsg;
}
private Stream CreateMIME(IBaseMessage pInMsg, string separator)
{
Stream mimeStream = new VirtualStream();
TextWriter writer = new StreamWriter(mimeStream);
// MIME-header
// form-data: Bodypart
writer.WriteLine(string.Format("--{0}", separator));
writer.WriteLine("Content-Disposition: form-data; name=\"data\"; filename=\"Dk_{5AA9547A-E103-40CD-A2F4-6B77F010E570}.xbrl\"");
writer.WriteLine("Content-Type: application/xml");
writer.WriteLine();
// Read BodyPart stream
using (StreamReader reader = new StreamReader(pInMsg.BodyPart.GetOriginalDataStream()))
{
while (!reader.EndOfStream)
{
writer.WriteLine(reader.ReadLine());
}
}
// form-data: email
writer.WriteLine(string.Format("--{0}", separator));
writer.WriteLine("Content-Disposition: form-data; name=\"email\"");
writer.WriteLine("Content-Type: text/plain");
writer.WriteLine();
writer.WriteLine("fsfsd@fsdf.com");
writer.WriteLine(string.Format("--{0}", separator));
writer.Flush();
mimeStream.Position = 0;
return mimeStream;
}
And now the request from Postman (working):
Content-Type: multipart/form-data; boundary=--------------------------035085236562027409735938
Content-Length: 299420
----------------------------035085236562027409735938
Content-Disposition: form-data; name="data"; filename="Dk_{5AA9547A-E103-40CD-A2F4-6B77F010E570}.xbrl"
Content-Type: application/xml
<?xml version="1.0" encoding="utf-8"?>
<!- ---- Lots of removed xbrl ---- ->
</xbrli:xbrl>
----------------------------035085236562027409735938
Content-Disposition: form-data; name="email"
Content-Type: text/plain
test@test.com
----------------------------035085236562027409735938--
And finally our current request from Biztalk to receiving system:
--26f685f3-5804-44dd-b76b-b3de6391bda1
Content-Disposition: form-data; name="data"; filename="Dk_{5AA9547A-E103-40CD-A2F4-6B77F010E570}.xbrl"
Content-Type: application/xml
<?xml version="1.0" encoding="utf-8"?>
<!- ---- Lots of removed xbrl ---- ->
</xbrli:xbrl>
--26f685f3-5804-44dd-b76b-b3de6391bda1
Content-Disposition: form-data; name="email"
Content-Type: text/plain
fsfsd@fsdf.com
--26f685f3-5804-44dd-b76b-b3de6391bda1
The below error message i everything I receive back:
A message sent to adapter "WCF-WebHttp" on send port "A_Sxxx_REST" with URI "https://xxx/xxx" is suspended.
Error details: System.Net.WebException: The remote server returned an unexpected response: (400) Bad Request.
{"error":{"status":400,"message":"HTTP 400 Bad Request"}}
MessageId: {DA019D0C-3C31-4C2C-BEA4-FBFCDF646984}
InstanceID: {B44ED7E8-67EE-4C35-BAC8-3D81553D6AA2}
So, does anyone know why the first part of the Postman request does not appear in the solution that we have programmed?