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
BBeairdBBeaird 

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="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:Respons>
         <Result>
            <ResponseXML><![CDATA[<RESPONSE>
            <DOCS_FOUND>1</DOCS_FOUND>
            <DOCS_EXTRACTED>1</DOCS_EXTRACTED>
                 </RESPONSE>]]></ResponseXML>
            <StatusXML>&lt;STATUS>SUCCESS&lt;/STATUS></StatusXML>
            <Documents>
               <IndexDetails/>
               <FileAttachmentList>
                  <FileName>12345.pdf</FileName>
                  <Attachment>
                     <xop:Include href="cid:12345@example.jaxws.sun.com" xmlns:xop="http://www.w3.org/2004/08/xop/include"/>
                  </Attachment>
               </FileAttachmentList>
            </Documents>
         </Result>
      </ns2:DocDNAExtractResponse>
   </S:Body>
</S:Envelope>

 

 

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*e488a5c6-7989-44e0-85d8-2b25c6d0fbb8@example.jaxws.sun.com>";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

--uuid:e488a5c0fbb8
Content-Id: <rootpart*e4fbb8@example.jaxws.sun.com>
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="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:DocResponse xmlns:ns2="http://mysite/"><Result><ResponseXML>&lt;RESPONSE>&#xd;
&lt;DOCS_FOUND>1&lt;/DOCS_FOUND>&#xd;
&lt;DOCS_EXTRACTED>1&lt;/DOCS_EXTRACTED>&#xd;
&lt;/RESPONSE>&#xd;
</ResponseXML><StatusXML>&lt;STATUS>SUCCESS&lt;/STATUS></StatusXML><Documents><IndexDetails></IndexDetails><FileAttachmentList><FileName>0001.pdf</FileName><Attachment><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:12cd3@example.jaxws.sun.com"/></Attachment></FileAttachmentList></Documents></Result></ns2:DocResponse></S:Body></S:Envelope>
--uuid:e488a5c6-7989-44e0-85d8-2b25c6d0fbb8
Content-Id: <12c5bcf4-441c-4a5d-910a-3181fafc4dd3@example.jaxws.sun.com>
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

%PDF-1.4
%âãÏÓ
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 =.