Posting Binary body (File Attachment) as POST request with i

Posted by Niall Morgan on 03-Nov-2017 02:44

I'm trying to use the SharePoint Office 365 API to send files to SharePoint.

I have managed to get the file to arrive in share point but it is corrupted when it gets there.I believe this is because the data needs to be sent in binary format and progress is sending it in some other format.

Could someone point me in the right direction on how to properly encode the post body as binary data ?

This is what i'm doing, but its not working properly.  Files arrive, but they have no data.

DEFINE VARIABLE encdmptr AS MEMPTR NO-UNDO.
DEFINE VARIABLE result AS CHARACTER NO-UNDO.
DEFINE VARIABLE encdlngc AS LONGCHAR NO-UNDO.
def var cWebServiceUrl as char no-undo.
def var oBody AS OpenEdge.Core.ByteBucket NO-UNDO.

/* im not sure this is correct */

COPY-LOB FROM FILE "C:\Users\me\Desktop\testupload.xlsx" TO encdmptr NO-CONVERT NO-ERROR.
encdlngc = BASE64-ENCODE(encdmptr).

cDocLib = "DOCLIB IN SHAREPOINT".



cWebServiceUrl = "yoursharepointsite.com/.../GetFolderByServerRelativeUrl('DOCLIB IN SHAREPOINT')/Files/add(url='testupload.xlsx',overwrite=true)".


/* post body params */

/* again this is probably not correct */
oBody = NEW OpenEdge.Core.ByteBucket(encdlngc).

/* send post request for token */
oRequest = RequestBuilder:Post(cWebServiceUrl, oBody)
:AddHeader("Accept","application/json;odata=verbose")
:AddHeader("Authorization", "Bearer " + cAccessToken)
:Request.

oResponse = ClientBuilder:Build():Client:Execute(oRequest).

IF oResponse:StatusCode <> 200 THEN
DO:
MESSAGE
oResponse:StatusCode SKIP
oResponse:StatusReason SKIP
VIEW-AS ALERT-BOX.
END.

/* parse response */
oJsonEntity = CAST(oResponse:Entity, JsonObject).

/* just for debugging */
Message oJsonEntity:GetJsonObject('d'):GetJsonObject('__metadata'):GetCharacter('id').

RETURN result.

Posted by Peter Judge on 03-Nov-2017 10:24

You can do something like the below. The important parts are

- don't BASE64-ENCODE the memptr

- specify  the content type (may not have one)

def var oBody as class OpenEdge.Core.Memptr.

COPY-LOB FROM FILE "C:\Users\me\Desktop\testupload.xlsx" TO encdmptr NO-CONVERT NO-ERROR.
oBody = new Memptr(encdmptr).
/* send post request for token */
oRequest = RequestBuilder:Post(cWebServiceUrl, oBody, '
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') // or whever's needed
:AcceptContentType("application/json;odata=verbose")
:AddHeader("Authorization", "Bearer " + cAccessToken)
:Request.
finally: set-size(endcdmptr) = 0. //cleanup end.

All Replies

Posted by Peter Judge on 03-Nov-2017 10:24

You can do something like the below. The important parts are

- don't BASE64-ENCODE the memptr

- specify  the content type (may not have one)

def var oBody as class OpenEdge.Core.Memptr.

COPY-LOB FROM FILE "C:\Users\me\Desktop\testupload.xlsx" TO encdmptr NO-CONVERT NO-ERROR.
oBody = new Memptr(encdmptr).
/* send post request for token */
oRequest = RequestBuilder:Post(cWebServiceUrl, oBody, '
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') // or whever's needed
:AcceptContentType("application/json;odata=verbose")
:AddHeader("Authorization", "Bearer " + cAccessToken)
:Request.
finally: set-size(endcdmptr) = 0. //cleanup end.

Posted by Niall Morgan on 06-Nov-2017 03:36

Hi Peter, Thanks for the help.

I got it to work with a few changes.

/* post body params */
oBody = new OpenEdge.Core.Memptr(encdmptr).
/* send post request for token */
oRequest = RequestBuilder:Post(cWebServiceUrl, oBody)
:AddHeader("Accept","application/json;odata=verbose")
:AddHeader("Content-Type","application/x-www-urlencoded; charset=UTF-8")
:AddHeader("Authorization", "Bearer " + cAccessToken)
:Request.

oResponse = ClientBuilder:Build():Client:Execute(oRequest).

Posted by Peter Judge on 06-Nov-2017 08:36

Either full-qualify this line

oBody = new OpenEdge.Core.Memptr(encdmptr).

Or add

            USING OpenEdge.Core.Memptr.

To the top of the program. PDSOE can help with this via the CTRL-SHIFT-O hotkey

 
 

This thread is closed