0

I am trying to upload a file by using REST API in salesforce but getting the above error.

public static PageReference  upload(String at,Blob b,String f,String ft){
            String access_token = at;
            blob file = b;
            String fName = f;
            String ftype = ft;

        System.debug('fileName '+fName);
        System.debug('FileType '+ftype);
        System.debug('Access token '+access_token);

        //req.setHeader('Content-Type ', String.valueOf(messageBody.length()));
        String boundary = '-------314159265358979323846';
        String delimiter = '\r\n--' + boundary +'\r\n';
        String close_delim = '\r\n--' + boundary + '--';
        String bodyEncoded = EncodingUtil.base64Encode(file);
        System.debug('body is '+bodyEncoded);

        String body = delimiter +'Content-Type: application/json\r\n\r\n' +'{ "title" : "'+ fName+'",' +' "mimeType" : "'+ ftype+ '" }'+delimiter +
       'Content-Type: ' + ftype + '\r\n' +'Content-Transfer-Encoding: base64\r\n' +'\r\n' +bodyEncoded +close_delim;

        HttpRequest req = new HttpRequest();
        req.setMethod('POST');
        req.setEndpoint('https://www.googleapis.com/upload/drive/v2/files/?uploadType=multipart');
        req.setHeader('Authorization', 'Bearer ' + access_token);
        req.setHeader('Content-Type ', 'multipart/mixed; boundary="'+boundary+'"');
        req.setHeader('Content-length', String.valueOf(body.length()));
        req.setHeader('Content-Encoding','base64');
        req.setBody(body);
        req.setTimeout(60*1000);

        Http h = new Http();
        HttpResponse res = h.send(req);
        System.debug('Response for upload is ' + res);
        return null;

Also, is there any documentation/resource where I can read more about the syntax for headers used in apex will be of much help.

Following is the error that I am getting on my page:

Visualforce Error
Help for this Page

System.CalloutException: Attempt to use invalid header in request
Error is in expression '{!upload}' in component <apex:commandButton> in page googledrive: Class.GoogleDriveHelper.upload: line 96, column 1
Class.GDrive.upload: line 67, column 1

Class.GoogleDriveHelper.upload: line 96, column 1
Class.GDrive.upload: line 67, column 1
  • Best you include the error verbatim and specify which line/ header the error is referring to – glls Jun 1 '18 at 11:44
  • 3
    You've got a spurious space after Content-Type - I'm not sure whether those get trimmed or not. – David Reed Jun 1 '18 at 11:45
  • @DavidReed They don't. HTTP protocol is quite strict on this matter. – sfdcfox Jun 1 '18 at 11:47
  • @DavidReed, Thanks your solution helped me out. But still I cannot see the uploaded file in my drive!(After making the changes as you said).It says Response for upload is System.HttpResponse[Status=Bad Request, StatusCode=400] – Krishnan Mishra Jun 1 '18 at 11:59
  • I don't know when your file is being converted to a blob, but something tells me that blob file = b; may need to be blob file = blob.valueOf(b); if you're passing a file name into your method with a mime-type where b is the file body. – crmprogdev Jun 2 '18 at 23:17

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.