How can I get my custom Apex Rest Services to use the CORS settings?
There are many similar questions like this one; but these were asked before CORS was supported.
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
As of the Summer '16 release CORS is available for Apex REST Classes. This is described in Use CORS to Access Salesforce Resources from Web Browsers.
These Salesforce technologies support CORS.
- Analytics REST API
- Bulk API
- Chatter REST API
- Salesforce IoT REST API
- Lightning Out
- REST API
- User Interface API
- Apex REST
In Salesforce, add the origin serving the code to a CORS whitelist. If a browser that supports CORS makes a request to an origin in the whitelist, Salesforce returns the origin in the Access-Control-Allow-Origin HTTP header, along with any additional CORS HTTP headers. If the origin is not included in the whitelist, Salesforce returns HTTP status code 403.
- From Setup, enter CORS in the Quick Find box, then select CORS.
- Select New.
- Enter an origin URL pattern.
The origin URL pattern must include the HTTPS protocol (unless you’re using your localhost) and a domain name and can include a port. The wildcard character (*) is supported and must be in front of a second-level domain name. For example, https://*.example.com adds all subdomains of example.com to the whitelist.
The origin URL pattern can be an IP address. However, an IP address and a domain that resolve to the same address are not the same origin, and you must add them to the CORS whitelist as separate entries.
Unfortunately at the moment CORS is still not available for custom REST endpoints. Spring '15 only made it available for the standard REST API.
This is mentioned (incredibly briefly) at the end of this Salesforce Developers Blog post.
Apex REST Methods are not accessible via CORS.
Fingers crossed they add it in a future release as this would solve some headaches in a couple of projects I'm working on.
Looks like Apex REST now supports CORS: http://blog.lkatney.com/2016/05/04/salesforces-summer-16-release-is-here/
Apex REST now supports CORS.
EDIT: Included relevant passages.
As of Summer 16, Apex REST now supports CORS: https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_apex_rest_cors.htm