Skip to content

Instantly share code, notes, and snippets.

@jovabe
Created June 30, 2017 11:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jovabe/e072545671c047243f0e2c80f8d137b4 to your computer and use it in GitHub Desktop.
Save jovabe/e072545671c047243f0e2c80f8d137b4 to your computer and use it in GitHub Desktop.
Salesforce Multipart/Form-data
public class ABCService {
@future(callout=true)
public static void addJobApplicationFuture() {
addJobApplication();
}
public static void addJobApplication() {
// Instantiate a new HTTP request
HttpRequest req = new HttpRequest();
// Set method and endpoint
req.setEndpoint('https://requestb.in/18so08t1');
req.setMethod('POST');
// Set body
String boundary = '----------------------------' + String.valueOf(DateTime.now().getTime());
String body = '--' + boundary + '\r\n';
body += 'Content-Disposition: form-data; name="api_access_token"\r\n\n';
body += 'theaccesstoken\r\n';
body += '--' + boundary + '\r\n';
body += 'Content-Disposition: form-data; name="api_version"\r\n\n';
body += '1\r\n';
body += '--' + boundary + '\r\n';
body += 'Content-Disposition: form-data; name="jobapplication"\r\n\n';
body += createJobAppJSONString() + '\r\n';
body += '--' + boundary + '--';
req.setBody(body);
// Set headers
req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
req.setHeader('Content-Length', String.valueof(body.length()));
system.debug('req: ' + req);
system.debug('req.header.contenttype: ' + req.getHeader('Content-Type'));
system.debug('req.header.contentlength: ' + req.getHeader('Content-Length'));
system.debug('req.body: ' + req.getBody());
// Send HTTP request and get HTTP response
Http http = new Http();
HttpResponse res = http.send(req);
system.debug('res: ' + res.getBody());
}
private static String createJobAppJSONString() {
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('channel', 'website');
gen.writeStringField('firstname', 'jef');
gen.writeStringField('lastname', 'klaas');
gen.writeStringField('timestamp', DateTime.now().format('dd/mm/yyyy hh:mm'));
gen.writeStringField('office_id', '2');
gen.writeEndObject();
return gen.getAsString().remove('\r').remove('\n');
}
}
@viviramji
Copy link

viviramji commented Apr 14, 2021

For me works using just one break line. In the body:

// rest of the code
String boundary = '----------------------------' + String.valueOf(DateTime.now().getTime());
String body = '--' + boundary + '\r\n';
body += 'Content-Disposition: form-data; name="<Key1>"\r\n';
body += 'value1\r\n';
body += '--' + boundary + '\r\n';
body += 'Content-Disposition: form-data; name="<Key2>"\r\n';
body += 'value2\r\n';
// rest of the code```

@zainilyasi-DLT
Copy link

zainilyasi-DLT commented May 26, 2022

I am getting the following error when sending a request:
Unexpected end of stream. The content may have already been read by another component.
Please Help!!!!
@viviramji @jovabe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment