Created
June 30, 2017 11:26
-
-
Save jovabe/e072545671c047243f0e2c80f8d137b4 to your computer and use it in GitHub Desktop.
Salesforce Multipart/Form-data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
} |
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
For me works using just one break line. In the body: