1

I am using exact answer from this link, just with some minor modification.

Post multipart without Base64 Encoding the body

I have developed a spring boot application which accepts two formdata, one is multipart as param and the other is string . I have marked attributes both as required 'true'.

I have hosted the app in heroku so that you all can also try. It works well with any rest client like this:

enter image description here

My Apex version :

public static void uploadFile(Blob file_body, String file_name, String reqEndPoint){
    String boundary = '----------------------------741e90d31eff';
    String header = '--'+boundary+'\r\nContent-Disposition: form-data; name="file";metadata="test me Apex"; filename="'+file_name+'"\r\nContent-Type: application/octet-stream'; // added '\r's removed ';' see Tim Smith's comment
    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;
        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;
        footer = '\n' + footer;
        String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
        bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
    } else {
        footer = '\r\n' + footer;
        String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
        bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
    }

    HttpRequest req = new HttpRequest();
    req.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
    req.setMethod('POST');
    req.setEndpoint(reqEndPoint);
    req.setBodyAsBlob(bodyBlob);
    req.setTimeout(120000);

    Http http = new Http();
    HTTPResponse res = http.send(req);
    System.debug('res -> '+res.getBody());
}

When I try to hit it via Anonymous apex:

Attachment attachment = [SELECT Id, Body, Name FROM Attachment WHERE Id = '00P7F00000BnUX2UAN'];
Test.uploadFile(attachment.Body, attachment.Name, 'https://localrestapi.herokuapp.com/consumeFiles');

The debug statement print like this :

21:15:37.942 (1942954579)|USER_DEBUG|[108]|DEBUG|res -> <html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Thu Jul 04 15:45:38 UTC 2019</div><div>There was an unexpected error (type=Bad Request, status=400).</div><div>Required String parameter &#39;metadata&#39; is not present</div></body></html>
1

I found a post and example here :

Multipart form data post file with multiple form parameters?

To post multiple form data I used this and it worked . basically we have to add boundary after each form data.

public static void uploadFile(Blob file_body, String file_name, String reqEndPoint){

    //callout ePOR service
    string contentType = vNHttpFormBuilder.GetContentType();

    //  Compose the form
    string form64 = '';
    //******This was the boundary I was missing****
    form64 += vNHttpFormBuilder.WriteBoundary();
    form64 += vNHttpFormBuilder.WriteBodyParameter('metadata', 'Test me');
    form64 += vNHttpFormBuilder.WriteBoundary();
    form64 += vNHttpFormBuilder.WriteBlobBodyParameter('filename', EncodingUtil.base64Encode(file_body), file_name);
    form64 += vNHttpFormBuilder.WriteBoundary(null);
    blob formBlob = EncodingUtil.base64Decode(form64);
    HttpRequest req = new HttpRequest();
    req.setMethod('POST');
    req.setEndpoint(reqEndPoint);
    req.setHeader('Content-Type', contentType);
    req.setBodyAsBlob(formBlob);
    req.setTimeout(120000);

    Http http = new Http();
    HTTPResponse res = http.send(req);
    System.debug('res -> '+res.getBody());
}

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.