4

We have integrated our Salesforce instance with one of the third party app and now they want to send us a pdf file as a response in Multipart format. Below is the sample of incoming response. We want to use this Multipart 'Content-Type' data and create/upload a pdf file to the related object in Salesforce. I was wondering if someone has experience in parsing this and can help me to understand how can I process this Multipart 'Content-Type' in Apex REST or REST API. Any help is much appreciated.

Header
Content-Length=161143&Content-Type=multipart%2fform-data%3b+boundary%3d----------------------------6d848496489849&Expect=100-continue&Host=localhost%3a1251

POST DATA


05003000000D8cuI251515afagwgagaw
------------------------------6d848496489849
Content-Disposition: form-data; name="WidgetID";

Hello
------------------------------6d848496489849
Content-Disposition: form-data; name="Widgetname";

World
------------------------------6d848496489849
Content-Disposition: form-data; name="jsonData"; filename="jsonData"
Content-Type: application/json


{"widget":{"debug":"on","window":{"title":"Sample Konfabulator Widget","name":"main_window","width":500,"height":500},"image":{"src":"Images/Sun.png","name":"sun1","hOffset":250,"vOffset":250,"alignment":"center"},"text":{"data":"Click Here","size":36,"style":"bold","name":"text1","hOffset":250,"vOffset":100,"alignment":"center","onMouseUp":"sun1.opacity = (sun1.opacity / 100) * 90;"}}}
------------------------------6d848496489849


Content-Disposition: form-data; name="pdffile"; filename="pdffile"
Content-Type: application/pdf

%PDF-1.0
… PDF file…
------------------------------6d848496489849
2
  • So you want to receive and process this data in Apex as a custom REST API? May 31, 2017 at 21:38
  • I'm wondering if you could use a Visualforce page to process the request. In the backing controller the getParameters() could be used to automatically split apart the parts for you. May 31, 2017 at 21:48

1 Answer 1

4

The REST API does support multipart requests, but you would need to follow their requirements for inserting new records - Insert or Update Blob Data.

Apex REST Methods don't currently (Summer '17) support multipart:

Apex REST currently doesn't support requests of Content-Type multipart/form-data. Source


One option for a custom form would be to use a Visualforce page to handle the POST request and decode the multipart data for you.

UPDATE: Unfortunately this is unable to access the actual PDF blob.

I made a very rough Visualforce page that dumped the contents of ApexPages.currentPage().getParameters() back to the page.

Visualforce page

<apex:page controller="Multipart_Controller" cache="false" showHeader="false" sidebar="false">
    <!-- Just dump the contents to show it works. 
         escape="false" is otherwise not a good idea here! -->
    <pre>
        <apex:outputText escape="false" value="{!parameterDetails}"></apex:outputText>
    </pre>
</apex:page>

Controller

public without sharing class Multipart_Controller {

    public String parameterDetails {
        get {

            if(parameterDetails == null) {
                parameterDetails = '';

                if (ApexPages.currentPage() != null){

                    Map<String, String> formData = ApexPages.currentPage().getParameters();
                    for (string key : formData.keySet()){
                        if (formData.get(key) != null){
                            parameterDetails += key + ' = ' + formData.get(key) + '\n';
                        }
                    }

                }
            }
            return parameterDetails;
        }
        private set;
    }

}

I did a test post and could see most of the POSTed form data. Note that this is a browser based tool which would have included my active cookies with sid in the request as well.

enter image description here

From there you could extend the Visualforce page with an init call to a controller method that would store the form data on a Custom Object with the required fields. You could then process the form data as required. Assuming you couldn't just process it directly in the controller within the heap limits.

10
  • Thank you so much Daniel. This is very helpful. One more question, which Endpoint/URL should be given to the third party application to make this post request? In your Postman screenshot above, you have included /apex/Endpoint. Is this the endpoint I should use?
    – Wendy
    Jun 1, 2017 at 2:43
  • @Wendy /apex/Endpoint is the URL for the Visualforce page. Only because I called my Visualforce page Endpoint. It would also pay to check that you can access the binary PDF data from the pdffile form data with the Apex controller. Note that they would also need to include a valid sid cookie to allow calling the Visualforce page. Jun 1, 2017 at 2:48
  • Thanks Daniel. I tried this on postman and it worked. I guess I wasnt exposing my VF page on Sites which was causing the issue. One last question, when you say "init call to a controller", what does that mean? Could you please give me an example? Apologies for so many question, and much appreciate your help.
    – Wendy
    Jun 1, 2017 at 3:18
  • I have tried few things but still getting pdf if below string format. Could you please help me understand how can I upload this PDF as "Attachment" or "ContentVersion". As per your suggestion, Im storing this data in a custom object but pdf file gets stored as 'dffile.org.eclipse.jetty9.servlet.contentType = application/pdf string'. Not sure how this can be used as actual file? ** pdffile = rightbridgeforsalesforce.pdf pdffile.org.eclipse.jetty9.servlet.contentType = application/pdf **
    – Wendy
    Jun 1, 2017 at 10:08
  • 1
    Thanks Daniel. I agree, processing blob is the only challenge I see. If you are able to find a better way, please let me know. Thank you again for all the help!
    – Wendy
    Jun 2, 2017 at 12:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .