Skip to content

Instantly share code, notes, and snippets.

@hugohernandezfcc
Created August 8, 2019 06:36
Show Gist options
  • Save hugohernandezfcc/d82bfb56cd8bc92c44ded830e6037612 to your computer and use it in GitHub Desktop.
Save hugohernandezfcc/d82bfb56cd8bc92c44ded830e6037612 to your computer and use it in GitHub Desktop.
csv file httprequest not work
public class JSONMSAccess {
public String token_type;
public String expires_in;
public String ext_expires_in;
public String expires_on;
public String not_before;
public String resource;
public String access_token;
}
HttpRequest req = new HttpRequest();
req.setEndpoint('https://login.microsoftonline.com/001c09d5-d107-4fac-ac9c-cf2137501098/oauth2/token');
String payLoad = 'grant_type=client_credentials&client_id=7b85ee92-d478-4ce8-80cc-7088292ae9d5&client_secret=[M/8[u*3GjU8Brfqjbcl2-LX4En0ZN:X';
req.setMethod('POST');
req.setBody(payLoad);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
JSONMSAccess msAccess = (JSONMSAccess) System.JSON.deserialize(res.getBody(), JSONMSAccess.class);
System.debug(msAccess.access_token);
ContentVersion cv = [select id, ContentDocumentId, versiondata, Title From Contentversion where ContentDocumentId='069r0000001YUIlAAO' ];
Blob csvFileBody = cv.VersionData;
String boundary = '----------------------------741e90d31eff';
String header = '--'+boundary+'nContent-Disposition: form-data; name="file"; filename="'+cv.Title+'.csv";nContent-Type: application/octet-stream';
String footer = '--'+boundary+'--';
String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'rnrn'));
while(headerEncoded.endsWith('=')){
header+=' ';
headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'rnrn'));
}
String bodyEncoded = EncodingUtil.base64Encode(csvFileBody);
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 = 'rn' + footer;
String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
}
HttpRequest req2 = new HttpRequest();
req2.setEndpoint('https://mbp.azure-api.net/cub/mbdcp/v1/api/add/bulk');
req2.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
req2.setHeader('Authorization', msAccess.access_token);
req2.setHeader('AZ-sub-key', 'b0e67f14532d4c71bcb59893cce66d8d');
req2.setHeader('Accept', 'application/json');
req2.setMethod('POST');
//req2.setBodyAsBlob(bodyBlob);
//req2.setHeader('Content-Length',String.valueof(req2.getBodyAsBlob().size()));
req2.setbody('------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"couponEndDate\"\r\n\r\n'+'variable1'+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"couponStartDate\"\r\n\r\n'+'variable2'+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n'+'variable3'+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"tpevAgentId\"\r\n\r\n'+'variable4'+'\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--');
req2.setTimeout(120000);
Http http2 = new Http();
HTTPResponse res2 = http.send(req2);
System.debug(res2.getBody());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment