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
Content-Type
- I'm not sure whether those get trimmed or not. – David Reed♦ Jun 1 '18 at 11:45blob file = b;
may need to beblob 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