Sending a PDF file in a REST Message

278 Views Asked by At

I am trying figure this out any help appreciated. Thanks in advance

The service is a file passthrough that picks up a PDF and then uses the BPL code below to create the rest wrapper and sends the stream data. I am not grabbing the stream from the inbound correctly, "request.Stream" or "request.StreamFC" always produces the following error.

Here is what is sent and returned in the message..

request:

*

<Stream>
    15@%Library.FileCharacterStream
    </Stream>
    response:
    <Stream>
    Unable to load xml from message: Data at the root level is invalid. Line 1, position 1.
    </Stream>

-I think my issue is I am not using the right class by using "%GlobalStreamCharacter", Or the raw PDF CDATA inbound I am not handling correctly

set context.RESTMessage.Stream=##class(%GlobalCharacterStream).%New() 

I have tried using different Stream types...File, Binary....but no luck

-The general BPL below

/// BPL Definition X

Data BPL [ XMLNamespace = "http://www.intersystems.com/bpl" ]
    {
    <process language='objectscript' request='Ens.StreamContainer' response='Ens.Response' 
     height='2000' width='2000' >
    <context>

    .....

    .....

    ....

    <code name='Create REST Message' xpos='200' ypos='250' >
    <![CDATA[
       set context.RESTMessage=##class(EnsLib.HTTP.GenericMessage).%New()
       set context.RESTMessage.Stream=##class(%GlobalCharacterStream).%New()
       set tSC=context.RESTMessage.Stream.Write(request.StreamFC)
       set tSC=context.RESTMessage.HTTPHeaders.SetAt("application/xml","Content-Type")
       set tSC=context.RESTMessage.HTTPHeaders.SetAt(context.RESTMessage.Stream.Size,"Content- 
       Length")
       set tSC=context.RESTMessage.HTTPHeaders.SetAt("POST","HttpRequest")
    
   
       ]]>
    </code>
1

There are 1 best solutions below

1
On

You can try this code snippet. I got the answer in the community.

<call name='To application Rest' target='To application REST' async='0'>
  <request type='EnsLib.REST.GenericMessage' >
    <assign property="callrequest.Stream" value='##class(%Stream.GlobalCharacter).%New()'/>
    <assign property="status"  value='callrequest.Stream.CopyFrom(request.StreamFC)' action="set" />
    <assign property="status"  value='callrequest.HTTPHeaders.SetAt("application/xml", "Content-Type")' action="set" />
    <assign property="status"  value='callrequest.HTTPHeaders.SetAt(callrequest.Stream.Size, "Content-Length")' action="set" />
    <assign property="status"  value='callrequest.HTTPHeaders.SetAt("POST", "HttpRequest")' action="set" />
  </request>
</call>