BPM, Workflow, and Case

 View Only
Expand all | Collapse all

Multipart form data file upload from IBM BPM

  • 1.  Multipart form data file upload from IBM BPM

    Posted Wed April 14, 2021 04:10 PM
    Hi All,

    We are trying to upload document using rest services and the input is 'multipart form-data'.
    Has anyone of us worked on the same implementation, if Yes, please share as we are facing issue while forming the request from BPM

    Thanks in advance.

    ------------------------------
    Suraj Sawant
    ------------------------------


  • 2.  RE: Multipart form data file upload from IBM BPM

    Posted Wed April 14, 2021 07:36 PM
    Hi Suraj,

    You can always click on documentation link of BPM REST services to understand how to use them


    According to the docs (https://www.ibm.com/docs/en/baw/19.x?topic=instance-post-add-document), you can provide your document data to the rest service via 2 ways:
    - Via data params
    - Or using multipart form data directly in your request

    Method 1 - Via 'data' Param
    Rest URL: https://{{yourBPMHostName}}/rest/bpm/wle/v1/process/12672?action=addDocument&name=Test+Add+Doc+Via+Rest&hideInPortal=false&docType=file&parts=all&accept=application%2Fjson&override-content-type=text%2Fplain&data=test


    For demo purposes, my document content is simple, just a 'test' string. But in your case, you probably have to encode your file content into base64 string and provide the encoded string into 'data' param.

    Method 2 - Form a multipart form data in the http request

    So multipart/form-data is a content-type for uploading documents. For this, your POST http request should have a header like this

    Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryxACENuVAqBlsZ0XA

    For the boundary part, you can form any unique string. As for how to construct the body, you can read this post https://stackoverflow.com/questions/47080869/how-to-manually-create-multipart-form-data

    This is a good function to form the http request body for multipart/form-data

    function multiPost(method, url, formHash){
        var boundary = "nVenJ7H4puv"
        var body = ""
        for(var key in formHash){
            body += "--" + boundary
                 + "\r\nContent-Disposition: form-data; name=" + formHash[key].name
                 + "\r\nContent-type: " + formHash[key].type
                 + "\r\n\r\n" + formHash[key].value + "\r\n"
        }
        body += "--" + boundary + "--\r\n"
    
        var xml = new XMLHttpRequest();
        xml.open(method, url)
        xml.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary)
        xml.setRequestHeader("Content-Length", body.length)
        xml.send(body)
    }
    the 'formHash' is the file content you get from <input type='file'>, how to get that content you can read this as well 
    https://stackoverflow.com/questions/12281775/get-data-from-file-input-in-jquery

     You can test BPM rest service via Postman for method 2 as well

    Hope this helps
    Thanks


    ------------------------------
    Thong Huynh
    Sydney NSW
    ------------------------------