function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

HTTP Callouts Parsing Mixed XML and Binary Attachments

I'm doing a callout to a webservice that results in me getting a PDF file. The trick is the response contains some XML first followed by the PDF binary in an "attachment." I can't seem to figure out how to get Apex to reliably separate out just the binary bit. I also have not control over the webservice, so I can't just have them send me only the binary file. All the documentation I've found only applies for converting an HTTP resonse body directly into a blob, which won't work for me because I need the blob to ignore on the non-binary stuff at the beginning of the request. How can Salesforce handle xml attachments like this?  I've also tried just base64 encoding the entire response into a string, but I can't figure out how to reliably parse down to just the PDF section.


If I do a test via  SoapUI, this is the XML view:


<S:Envelope xmlns:S="">
                     <xop:Include href="" xmlns:xop=""/>



Here's the raw view (the binary I care about starts at the %PDF-1.4 line):


HTTP/1.1 200 OK
Date: Tue, 19 Feb 2013 14:04:29 GMT
Transfer-Encoding: chunked
Content-Type: multipart/related;start="<rootpart*>";type="application/xop+xml";boundary="8";start-info="text/xml"
Set-Cookie: JSESSIONID=tGv1R!-1853; path=/; secure; HttpOnly
X-Powered-By: Servlet/2.5 JSP/2.1

Content-Id: <rootpart*>
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"
Content-Transfer-Encoding: binary

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S=""><S:Body><ns2:DocResponse xmlns:ns2="http://mysite/"><Result><ResponseXML>&lt;RESPONSE>&#xd;
</ResponseXML><StatusXML>&lt;STATUS>SUCCESS&lt;/STATUS></StatusXML><Documents><IndexDetails></IndexDetails><FileAttachmentList><FileName>0001.pdf</FileName><Attachment><xop:Include xmlns:xop="" href=""/></Attachment></FileAttachmentList></Documents></Result></ns2:DocResponse></S:Body></S:Envelope>
Content-Id: <>
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

4 0 obj <</Length 6778/Filter/FlateDecode>>stream
<<Lots more PDF binary stuff after this part>


ad 7ad 7
Hello BBeaird,

I am having similar issue, were you able to solve the problem that you had ? Any help would be appreciated. 

Thank you =.