We are running into an issue making a Cross Origin call to the Bonita API. The issue we are having is similar to the problem described in the linked post but the solution to that question is not fixing the problem in our current environment:
http://community.bonitasoft.com/answers/cors-ajax-bonita-rest-ap-and-tomcat7
What I am seeing is when I make a call that causes a preflight (OPTIONS) request it is failing with a 401 Unauthorized response in Firefox. It is working in Chrome; however, I believe that is because of an issue in Chrome and Firefox is actually doing what it is supposed to do which causes the failure (this should make more sense as you read on).
Here is the request in Chrome. Notice the cookie:
OPTIONS /bonita/API/bpm/humanTask?p=0&c=100&f=state=ready HTTP/1.1
Host: X.X.X.X:8080
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://myworkstation.company.com:8082
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: /
Referer: http://myworkstation.company.com:8082/index.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: JSESSIONID=EF06ABFF189A89F8D95791F44BD7B8C3; BOS_Locale=en
And here is the response in Chrome:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: http://myworkstation.company.com:8082
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, HEAD, OPTIONS, POST, DELETE, PUT
Access-Control-Allow-Headers: Content-Type, Accept
Content-Length: 0
Date: Wed, 02 Jul 2014 12:45:24 GMT
Here is the request in Firefox. No cookie is passed:
OPTIONS /bonita/API/bpm/humanTask?p=0&c=100&f=state=ready HTTP/1.1
Host: X.X.X.X:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:30.0) Gecko/20100101 Firefox/30.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://myworkstation.company.com:8082
Access-Control-Request-Method: GET
Access-Control-Request-Headers: content-type
Connection: keep-alive
Here is the response in Firefox:
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=DC1E9B22973D2EF3EBB587E11BCD03BC; Path=/bonita/; HttpOnly
Content-Type: text/html;charset=utf-8
Content-Length: 951
Date: Wed, 02 Jul 2014 12:40:49 GMT
I believe the reason this is working in Chrome is because Chrome is passing the Cookie; however, according to w3.org, a CORS preflight request shouldn’t expect a cookie; which makes sense. A preflight call is a call to determine if an action is allowed. It should not require credentials to determine if I can do something, it should only require credentials to actually do it.
This is similar to an issue seen in the Twitter API which may explain the problem better than I am: https://code.google.com/p/twitter-api/issues/detail?id=2273
Finally, I realize this is a GET request and the only reason that it is causing a preflight is because I am setting the Content-Type header to application/json. I could fix this particular instance of the issue by removing the Content-Type on the GET; however, I am still making post calls at some point and if I do not specify the Content-Type as application/json on my posts (i.e. the API/bpm/case call in order to start a case, see example below). Setting application/json as the Content-Type will always cause a preflight request so I do not see a way to avoid the issue.
var startCase = function startCase(processId){
return $.ajax({
xhrFields: {withCredentials: true},
contentType: “application/json”, //Required or you get a 500 error
url: bonitaUrl + “API/bpm/case/”,
type: ‘POST’,
data: ‘{“processDefinitionId”:"’ + processId + ‘"}’
});
}
This can be solved (from what I can tell) if Bonita changes the API so that a preflight (OPTIONS) request does not require authorization.