Posting Binary body (File Attachment) as POST request with iHTTP.net library - Forum - OpenEdge Development - Progress Community

Posting Binary body (File Attachment) as POST request with iHTTP.net library

 Forum

Posting Binary body (File Attachment) as POST request with iHTTP.net library

This question is answered

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.

Verified Answer
  • 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
  • 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.
  • 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).

  • 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