function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Test Dev 81Test Dev 81 

STATUS 415 Unsupported Media Type while uploading text file using postman

hi everyone,

I want to upload the file using apex but before do that I am testing callout by the postman
but I am getting error while making a POST request
User-added imagedo you have any idea what I am doing wrong here
thanks,
Rahul
Omar Rajab 94Omar Rajab 94
Hi ,

Yor are using the wrong Content-Type in Postman, it should be multipart/form-data instead of text/plain.

To Post the file with Apex Code see the following example:
Post File with Apex (https://gist.github.com/jovabe/e072545671c047243f0e2c80f8d137b4)

please mark as the best answer of this help you.

regards,
Omar
Test Dev 81Test Dev 81
Hi tried the example given in the link you posted, but it is giving me an error response BAD Request 400
DEBUG|res: "No files received."
 
public class ABCService2 {
    @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://saas.docufree.com/dfwebservice/v3/files/capture/folder?targetId=3486');
        req.setMethod('POST');
        String bodyStr;
        bodyStr='Opportunity Name'+'|'+'Stage'+'|'+'Opportunity Id'+'\n';
        bodyStr+='MY OPPORTUNITY |OPEN|0064W00000wrlJc';

        // 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 += Blob.valueOf(bodyStr) + '\r\n';
        body += '--' + boundary + '--';
        req.setBody(body);
        Rg_FileUploadService obj = new Rg_FileUploadService();
        string accessToken = obj.genAccessToken();
        req.setHeader('Authorization','Bearer ' + accessToken);

        // 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);
        system.debug('res: ' + res.getBody());
    }
    }

 
Omar Rajab 94Omar Rajab 94
try this code please: 
String fileName = 'MY_FILE.txt';
String fileContent =  'Opportunity Name'+'|'+'Stage'+'|'+'Opportunity Id'+'\n'+
        'MY OPPORTUNITY |OPEN|0064W00000wrlJc';
String targetURL = 'https://saas.docufree.com/dfwebservice/v3/files/capture/folder?targetId=3486';
String separationString = 'A_RANDOM_STRING';

// assemble the body payload
String header = '--' + separationString + '\nContent-Disposition: form-data; name="file"; filename="' + fileName + '"\nContent-Type: application/octet-stream\n\n';
String body = EncodingUtil.base64Encode(fileContent) + '\n';
String footer = '--' + separationString + '--';
String bodyPayload = header + body + footer;

// send out the request
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'multipart/form-data; boundary=' + separationString);
req.setHeader('Content-Length', String.valueof(bodyPayload));
req.setMethod('POST');
req.setEndpoint(targetURL);
req.setBody(bodyPayload);

Http http = new Http();
http.send(req);

reference: https://docparser.com/blog/post-file-salesforce-apex-external-http-webservices/

Regards,
Omar
Test Dev 81Test Dev 81
Thank you for the replay,
Now I am getting an error
Line: 47, Column: 1
System.CalloutException: Illegal character(s) in message header value: --A_RANDOM_STRING Content-Disposition: form-data; name="file"; filename="Grand Hotels SLA 2_2021_04_18_03_07_25.txt" Content-Type: application/octet-stream T3Bwb3J0dW5pdHkgTmFtZXxTdGFnZXxPcHBvcnR1bml0eSBJZApHcmFuZCBIb3RlbHMgU0xBIDJ8Q2xvc2VkIFdvbnwwMDY0VzAwMDAwd3VCWFRRQTI= --A_RANDOM_STRING--

 
Omar Rajab 94Omar Rajab 94
Here is the code in another way. i uesed it to send pdf: 
String fileContent = 'Opportunity Name'+'|'+'Stage'+'|'+'Opportunity Id'+'\n'+ 'MY OPPORTUNITY |OPEN|0064W00000wrlJc'; 
String targetURL = 'https://saas.docufree.com/dfwebservice/v3/files/capture/folder?targetId=3486';

String boundary = '----------------------------' + Datetime.now().format('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'');
		String header = '--' + boundary + '\r\nContent-Disposition: form-data; name="jobapplication"; filename="MY_FILE.csv"\r\nContent-Type: text/csv';
		String footer = '\r\n--' + boundary + '--';
		String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header + '\r\n\r\n'));
		while (headerEncoded.endsWith('='))
		{
			header += ' ';
			headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header + '\r\n\r\n'));
		}
		String bodyEncoded = EncodingUtil.base64Encode(Blob.valueOf(fileContent ));
		String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));

		Blob bodyBlob = null;
		String last4Bytes = bodyEncoded.substring(bodyEncoded.length() - 4, bodyEncoded.length());
		if (last4Bytes.endsWith('='))
		{
			Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
			HttpRequest tmp = new HttpRequest();
			tmp.setBodyAsBlob(decoded4Bytes);
			String last4BytesFooter = tmp.getBody() + footer;
			bodyBlob = EncodingUtil.base64Decode(headerEncoded + bodyEncoded.substring(0, bodyEncoded.length() - 4) + EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
		}
		else
		{
			bodyBlob = EncodingUtil.base64Decode(headerEncoded + bodyEncoded + footerEncoded);
		}

        HttpRequest req = new HttpRequest();
        req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
        req.setMethod('POST');
        req.setEndpoint(targetURL);
        req.setBodyAsBlob(bodyBlob);
        Http http = new Http();
        HTTPResponse res = http.send(req);

regards,
Omar