http client passing Bytes() as multi-part form

Posted by Rod Anderson on 14-Aug-2017 13:03

I have a requirement to pass a blob (file) "converted to an array of bytes".  From research in this community, I've tried a few things including System.IO.Memory Stream() as seen below.  I've also tried the method MemptrToByteArray() which is referenced in the community many times.  Regardless the appraoch I'm having difficulty getting the array to attached to the form.  If I copy it back to a memptr it converts to the original file?

Any insight would be appreciated.

Best Regards,

Rod

oMemStream = NEW System.IO.MemoryStream(iSize).

  DO ix = 1 TO iSize:

      oMemStream:WriteByte(GET-BYTE(mData,ix)).

  END.    

     
ASSIGN 
    oSoapDoc        = NEW MEMPTR(mData)
    oPart           = NEW MessagePart('application/octet-stream':u, oSoapDoc)
    oPart:ContentId = 'blob'  .
     
    
      oPart:Headers:Put(HttpHeaderBuilder:Build('Content-Disposition')
                                               :Value('form-data; name="blob"; filename="QCDSC221.xml"')
                                               :Header).
 
oMultiEntity:AddPart(oPart).

Posted by Peter Judge on 14-Aug-2017 15:19

I think you're doing the right thing with COPY-LOB ... the fact that you're loading SOAP messages and that they're all readable bytes (characters) may be throwing you off. Try COPY-LOB with (say) a PNG or a PDF and you may see more "byte-ey" stuff.

I added an example of a multipart message with 2 JSON, 1 text and 1 image part at https://github.com/PeterJudge-PSC/http_samples/blob/master/http_client/multipart_messages/multipart_img_json_txt.p .

HTH

All Replies

Posted by Peter Judge on 14-Aug-2017 13:24

The easiest way in ABL (and platform neutral) is to use the COPY-LOB statement.
 
DEF VAR mBook AS MEMPTR.
DEF VAR oBlob AS CLASS OpenEdge.Core.Memptr.
 
// Read from disk to primitive memptr
COPY-LOB FILE c:/temp/book.pdf TO mBook.
 
// Put into a form the HTTP client can use
oBlob = NEW Memptr(mBook).
 
// no leaks
SET-SIZE(mBook) = 0.
 
// Then do what you’re doing.
 

Posted by Rod Anderson on 14-Aug-2017 14:42

Peter,  I was doing almost this exact thing initially.  My request-raw.txt was showing the CLOB file and I was expecting to see bytes.   Must be my lack of understanding.   One quick follow up question as I'm trying to debug another 400 error.  

All the examples show using a memptr to populate a part of the form.  Is this the only object that can be used?  Can you pass a char or longchar as plain/text in a form part?  I couldn't find  a refence in the documention?  Thanks in advance,  Rod  

Posted by Peter Judge on 14-Aug-2017 15:19

I think you're doing the right thing with COPY-LOB ... the fact that you're loading SOAP messages and that they're all readable bytes (characters) may be throwing you off. Try COPY-LOB with (say) a PNG or a PDF and you may see more "byte-ey" stuff.

I added an example of a multipart message with 2 JSON, 1 text and 1 image part at https://github.com/PeterJudge-PSC/http_samples/blob/master/http_client/multipart_messages/multipart_img_json_txt.p .

HTH

This thread is closed