TransWikia.com

How to Send Salesforce Document to External API using POST

Salesforce Asked by Davis Handler on December 17, 2021

I need to send a .json file to an external API. I am creating this .json file using the Document object and then inserting it into the database.

The external API shows the following documentation via curl:

curl -i -F secret_key=<SECRET_KEY> -F public_key=<PUBLIC_KEY>
-F upload=@/home/user/test.txt 
https://www.thisisaplaceholder.com/api/2/resources/file

the upload value is the path to the specific file.

What is the best way to do this via Salesforce? I have tried the following approach but its not working:

  1. Run a SOQL Statement getting the document object:

Document doc = [SELECT Body FROM Document WHERE ID =:documentId];

  1. Add the body (Blob) to my payload:

String payload = 'uplaod=' + doc.body + '&secret_key=123' '&public_key=123';

  1. Set request.body = (payload)

  2. I receive the following response back from the API
    Unable to upload file: file must not exceed 30MB and must not be empty, request_uuid

My files are not 30mb so they are clearly not getting uploaded. My next thought would be to use the document uri path as the upload value.

2 Answers

After a week of banging my head in the wall, I was able to find a solution. I used this blog http://enreeco.blogspot.com/2013/01/salesforce-apex-post-mutipartform-data.html as my north star. Basically, you need to create your own header and footer and not use the default methods apex gives you.

Answered by Davis Handler on December 17, 2021

I have an example I put together where I used a File to achieve this if that will work for you. This reads the content from the File and sends the content to a Heroku app I'm borrowing from a certain Trailhead tutorial.

public static HttpResponse makePostCallout() 
{
    Http http = new Http();
    
    HttpRequest request = new HttpRequest();
    request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
    request.setMethod('POST');
    request.setHeader('Content-Type', 'application/json;charset=UTF-8');
    
    ContentVersion file = [SELECT VersionData FROM ContentVersion WHERE Title = 'Test Document' Limit 1];
    System.debug(file.VersionData.toString());
    request.setBodyAsBlob(file.VersionData);
    
    HttpResponse response = http.send(request);
    if(response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' + 
                    response.getStatusCode() + ' ' + response.getStatus());
    } else {
        System.debug(response.getBody());
    }
    
    return response;
}

"Test Document" Content:

{"name":"mighty moose"}

I should note, that this only works because the document's type is a .txt. This won't work for binary type files. More info about that here: https://developer.salesforce.com/forums/?id=906F0000000901FIAQ

Answered by Tim Hunter on December 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP