I am attempting to build an Http Request for a Post call to an API. The API documentation states I should send a content type of:
multipart/mixed;type="text/xml";boundary="MIME-Boundary"
It provides this example (redacted for length):
--MIME-Boundary
Content-Type: Application/XML
Content-Location: RN-Service-Content
Content-ID: pidxv10_invoice_xml[xml content redacted]
--MIME-Boundary
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-ID: Attachment1[pdf base 64 content redacted]
--MIME-Boundary--
I stumbled upon the class MultiPartFormDataContent. I feel like this has gotten me close but I don't know if this is correct. If it is the class I should use I'm uncertain how to use it to match the provided example. I'm unit testing this code:
public HttpRequestMessage BuildRequest(Models.InvoiceDTO invoiceDto, string pdfPath)
{
Invoice openInvoice = invoiceDto.MapOpenInvoiceData();
string serializedInvoice = openInvoice.Serialize();
string base64PDF = ConvertPdfFile(pdfPath);
MultipartFormDataContent formData = BuildContent(serializedInvoice, base64PDF);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, apiSettings.EndpointURL)
{
Content = formData
};
return request;
}
public string ConvertPdfFile(string filePath)
{
Byte[] bytes = File.ReadAllBytes(filePath);
return Convert.ToBase64String(bytes);
}
public MultipartFormDataContent BuildContent(string serializedInvoice, string base64PDF)
{
MultipartFormDataContent formData = new MultipartFormDataContent();
formData.Headers.Add("Content-ID", "Attachment1");
formData.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
formData.Add(new StringContent(base64PDF));
formData.Add(new StringContent(serializedInvoice, Encoding.UTF8, "application/pidx.v10+xml"));
return formData;
}
For the sake of visualizing what I have I am executing request.Content.ReadAsStringAsync().Result in a test harness. When I do that I get:
--df45d3bb-617f-44c3-8aa4-b0f1411571bc
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data[pdf content redacted]
--df45d3bb-617f-44c3-8aa4-b0f1411571bc
Content-Type: application/pidx.v10+xml; charset=utf-8
Content-Disposition: form-data[xml content redacted]
--df45d3bb-617f-44c3-8aa4-b0f1411571bc--
I have a GUID where the example has the text --MIME Boundary. Is "--MIME Boundary" supposed to be in my content or is this MIME Boundary an example placeholder?
For both the PDF and XML content, how do I set their respective properties for Content ID, Content Location, Content Transfer Encoding?