0

I want to expose a REST API to the other website. The REST API in salesforce basically needs to query on Opportunity data as well as Files related to Opportunity, zip it and return it.

Questions:

Is it possible to return zip file via REST API?

Are there any limitations to the above logic?

Thanks

  • Zip/unzip is a struggle in Apex as no API is exposed and Apex doesn’t include direct handling for bytes. Having said that, Google because there is at least one Apex implementation though likely to be slow so only good for small amounts of data. Why not just return JSON? – Keith C Sep 30 '19 at 19:30
  • ok so if I choose the JSON path, I can embed opportunities data as well as files inside it and return that list? After receiving the JSON list by third-party, the data can easily be converted into actual image/pdf document? – Anonymous Sep 30 '19 at 20:25
2

Is it possible to return zip file via REST API?

Possible, yes, wise, no. Neither Apex nor the Lightning Platform is especially well-suited to this kind of binary file manipulation, but some very clever people have built a library to manage ZIP files in Apex. (Disclaimer: I have not used it myself).

Are there any limitations to the above logic?

Yes. All of the ordinary governor limits apply. In this case, I suspect Heap Size may be the first one to bite you, since the string manipulation the library above must make in order to interact with the binary file content and perform compression is not memory-efficient. Or fast.

I would encourage instead returning from the API a list of ContentVersion Ids and having the remote system send a separate request to the native Salesforce REST API to retrieve the body of each, or implementing this API endpoint on a middleware platform that has fewer governor-style constraints.

| improve this answer | |
  • ok so if I go for the second option, what would be the return type of REST API method if I am returning opportunities data and contentversion Ids? Is there any sample code for that? – Anonymous Sep 30 '19 at 21:00
  • @Anonymous Presumably it would be a JSON response body that your application would define. Your use case is quite specific and I am not aware of any sample code that bears on it more closely than what you will find on Trailhead. – David Reed Sep 30 '19 at 21:13
  • I was able to retrieve Salesforce data and files links/ids. How do I make a second call to retrieve the actual file content? – Anonymous Oct 2 '19 at 18:22
4

If the client requests it, the response can be compressed using standard HTTP/1.1 semantics. From the docs:

To use compression, include the HTTP header Accept-Encoding: gzip or Accept-Encoding: deflate in a request. The REST API compresses the response if the client properly specifies this header.

Also see this Q&A for additional info: gzip Compression not working on custom REST API

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.