2

According to this documentation, the bulk API should now support CORS. However, I'm not finding that to be the case.

My the preflight request (OPTIONS) is failing with:

<?xml version="1.0" encoding="UTF-8"?><error
   xmlns="http://www.force.com/2009/06/asyncapi/dataload">
 <exceptionCode>InvalidSessionId</exceptionCode>
 <exceptionMessage>Unable to find session id</exceptionMessage>
</error>

Here is what the OPTION request looks like:

curl 'https://cs12-api.salesforce.com/services/async/40/job' -X OPTIONS -H 'Pragma: no-cache' -H 'Access-Control-Request-Method: POST' -H 'Origin: http://localhost:8080' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36' -H 'Accept: */*' -H 'Cache-Control: no-cache' -H 'Connection: keep-alive' -H 'Access-Control-Request-Headers: content-type,x-sfdc-session' --compressed

I've tried every combination of endpoint url I can think of. Always same result

Which makes no sense, because I'm pretty sure the token should not be required for the pre-flighted requests.

I can confirm that I properly have CORS setup because I am querying the Rest API and Custom Rest Endpoints right before this request fails.

HERE is a visual-force page to reproduce the error yourself. You need to add CORS Whitelist for Visualforce subdomain and endpoint URLS in javascript:

<apex:page >
    <script
      src="https://code.jquery.com/jquery-3.2.1.min.js"
      integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
      crossorigin="anonymous"></script>
    <script type="text/javascript">
        const __ACCESSTOKEN__ = '{!$Api.Session_ID}';
    function doRest(){
        console.log('running rest');
        $.ajax({
          type: "GET",
          url: 'https://na46.salesforce.com/services/data/v39.0/sobjects/Account',
          success: function( data ) {
            console.log(data);
            $(".rest").text( JSON.stringify(data) );
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
              console.log(XMLHttpRequest,textStatus, errorThrown)
              alert('ERROR: Check Console for details');
          },
          headers: {
            'Authorization': 'Bearer ' + __ACCESSTOKEN__
          },
          dataType: 'json',
        });
    }

    function doBulk(){
        console.log('running bulk');
        $.ajax({
          type: "POST",
          url: 'https://na46.salesforce.com/services/async/39/job',
          data: {
              "object":"account",
              "operation": "insert",
              "contentType": "JSON"
          },
          success: function( data ) {
            console.log(data);
            $(".bulk").text( JSON.stringify(data) );
          },
          error: function(XMLHttpRequest, textStatus, errorThrown) {
              console.log(XMLHttpRequest,textStatus, errorThrown)
              alert('ERROR: Check Console for details');
          },          
          headers: {
            'X-SFDC-Session': __ACCESSTOKEN__
          },
          dataType: 'json',
        });
    }



    </script>

    <button onclick="doRest();">
        Run Rest Request
    </button>

    <button onclick="doBulk();">
        Run Bulk Request
    </button>
    <hr/>
    <h2>
    Rest Result
    </h2>  
    <pre class="rest">

    </pre>
    <h2>
        Bulk Result
    </h2>
    <pre class="bulk">

    </pre>
</apex:page>
  • If I send the option request to the bulk API 2.0 endpoint it does return 200 – NSjonas Dec 1 '17 at 23:45
0

After many weeks salesforce finally confirmed it's a known issue.

| 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.