0

I am trying to upload any file to google drive using API from salesforce with the help of LWC (lightning web component). I am using multipart upload. But it's giving me error -

13:02:57:607 USER_DEBUG [27]|DEBUG|Response::: Bad content type. Please use multipart.

I have attached the code as well to get a better view of what I am trying to do -

public static String uploadFile(String base64Data, String fileName, String contentType) {
   System.debug('[FILE_TYPE] : '+contentType);

    String boundary = '-------';
    String delimiter = '\r\n--' + boundary + '\r\n';
    String close_delim = '\r\n--' + boundary + '--';
    String body = delimiter +'Content-Type: application/json\r\n\r\n' +'{ "title" : "' +fileName + '",' +  ' "mimeType" : "'+contentType +'" }' +delimiter + 'Content-Type: ' +contentType +'\r\n' + 'Content-Transfer-Encoding: base64\r\n' + '\r\n' + base64Data + close_delim; 
    // storageServerHelper();
    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('callout:'+tempList[0].Named_Credential__c+tempList[0].Endpoint_URL__c);
    req.setHeader('Content-Type', 'multipart/mixed; boundary ="' + boundary + '"');
    req.setHeader('Content-length', String.valueOf(body.length()));
    req.setBody(body);
    req.setMethod('POST');
    req.setTimeout(60 * 1000);
    HttpResponse resp = http.send(req);
    String res;
    res = resp.getBody();
    system.debug('Response::: '+res);

    return res;
} 

Your Answer

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

Browse other questions tagged or ask your own question.