0

To upload bank statements, send the Authorization Header as above and send the pk of a book you previously created and the file you want to upload as a "HTML multipart form (not a JSON)" to

cURL:

$ curl -X POST \ -H "Authorization: Basic dGVzdDZAb2Nyb2x1cy5jb206dGVzdGluZw==" \ -F 'pk=10399' \ -F 'upload=@../bank-statement.pdf' \

Apex Http Request:

public static String uploadFile(String method,Blob file_body, String file_name, String resourceName,Integer bookId){

    String response ='';
    try{
        String boundary = '----------------------------741e90d31eff';
        String header = '--'+boundary+'\nContent-Disposition: form-data; pk="'+bookId+'"; upload="'+file_name+'";\nContent-Type: multipart/form-data';//application/octet-stream

        System.debug('header>>> '+header);

        String footer = '--'+boundary+'--';             
        String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
        while(headerEncoded.endsWith('=')){
            header+=' ';
            headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'\r\n\r\n'));
        }


        String bodyEncoded = EncodingUtil.base64Encode(file_body);


        Blob bodyBlob = null;
        String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());


        if(last4Bytes.endsWith('==')) {

            last4Bytes = last4Bytes.substring(0,2) + '0K';
            bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
            // We have appended the \r\n to the Blob, so leave footer as it is.
            String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
            bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
        } else if(last4Bytes.endsWith('=')) {

            last4Bytes = last4Bytes.substring(0,3) + 'N';
            bodyEncoded = bodyEncoded.substring(0,bodyEncoded.length()-4) + last4Bytes;
            // We have appended the CR e.g. \r, still need to prepend the line feed to the footer
            footer = '\n' + footer;
            String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
            bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);              
        } else {
            // Prepend the CR LF to the footer
            footer = '\r\n' + footer;
            String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
            bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);  
        }

        HttpRequest req = new HttpRequest();

        String userName = ocrolusSettings.lendingxl__UserName__c;
        String password = ocrolusSettings.lendingxl__Password__c;
        Blob headerValue = Blob.valueOf(userName+ ':' +password);
        String authorizationHeader = 'Basic ' +EncodingUtil.base64Encode(headerValue);
        System.debug('authorizationHeader>>>'+authorizationHeader);
        req.setHeader('Authorization', authorizationHeader);

        req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
        req.setMethod(method);
        req.setEndpoint(ocrolusSettings.lendingxl__EndPoint__c+ resourceName);//
        //req.setEndpoint(reqEndPoint);
        req.setBodyAsBlob(bodyBlob);
        req.setBody('{"pk" : "'+bookId+'"}');
        req.setTimeout(120000);

        Http http = new Http();

        System.debug('req>>> '+req);
        HTTPResponse res = http.send(req);
        response =res.getBody();
        System.debug('response>>> '+response);
        //return res.getBody();
    }catch(Exception e){
        System.debug('Error Message >>> '+e.getMessage());
        System.debug('Error Message >>> '+e.getStackTraceString());
    }
    return response;
} 

Above code is what I have tried but not able to upload the file.

Response what I receive when Http request is made

{"status": 400, "message": "required key not provided @ data['pk']", "code": 1003, "meta": {"status": 400, "msg": "required key not provided @ data['pk']", "code": 1003}, "response": null}

  • 1
    So, what kind of issue do you have with that? – kurunve Feb 28 '18 at 8:03
  • Want the Http request for above curl request. – Ritesh Feb 28 '18 at 10:04
  • I can recommend you to debug your requests from curl and apex using url from requestb.in to see what are you actually sending from curl and apex, and perform necessary adjustments – kurunve Feb 28 '18 at 10:54
1

on multipart requests the body contains the whole data including the blob separated by the boundary you specified in the request header. You can find the specification here.

If you don't wan't to struggle around implementing default protocol specification just use ffhttp-core

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.