OPTIONS verb not implemented by OpenWhisk REST API to invoke actions
Hi,
I've been working on a OpenWhisk action that I want to call directly from the web browser. But I got an issue using jQuery.ajax. JQuery is sending a preflight OPTIONS method to check for cross-domain requests. As OpenWhisk does not emit proper CORS headers today, the AJAX call failed.
Could OpenWhisk REST API implement OPTIONS and send the right CORS headers?
This is not a lot of work to support with spray. There is an option directive already and the implementation of a CORS trait/directive shouldn't be more than 30 lines. With tests, < 100 lines.
Suggested solution:
- we add option calls to the
routes()inRestAPIs.scala. With ajax and Cross-domain it is not possible to pass the Authentication-Header (on OPTION-request). - Because we do not know if the user is allowed to get information about this action, we only respond with the wildcard for allowed origins and the following headers on OPTION calls:
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Authorization,content-type
The second header is needed to allow Authorization- and content-type header on second call.
- The OPTION-call is followed by the normal POST call. This request includes the Authorization. We do send
Access-Control-Allow-Origin: *in the response again. We also decided not to set a custom URL, because this would only have effect on calls from web browsers.
This request includes the Authorization. We do not send any headers in the response, because this request is already allowed because of the header from OPTION response.
This implies, that the user wants to make a call that is "preflighted" by an OPTIONS request, right? That will be true for action invocations, since they make a POST with Content-Type application/json. ~~GET on /actions for example won't be preflighted, hence it won't get the correct Access-Control headers?~~ They will in-fact be preflighted because we need to send Authorization as you mentioned.
I updated my comment above. We need to send the Header in POST response again. That was my mistake. We would suggest to send the header only on post requests. Or does anyone think, that we should also support getting and updating entities?
I don't see any harm in supporting the whole API and I as a user would expect that i can do all of the API operations from any client.
This would be much easier. We commit our current state for review, and if it's OK, we will write our tests.
https://github.com/openwhisk/openwhisk/compare/master...ChrBi:corsHeader?expand=1&w=1
Closed by https://github.com/openwhisk/openwhisk/commit/aea64518e44fa4a948617a5d015cd75a7c18bc2d.
@ChrBi working on missing tests
@csantanapr you know if that is documented? can you make a proposal where to document pls?
@jeremiaswerner OPTIONS is not documented since it's not implemented.
Need to documented in two places
- In swagger json file for the API
- It would be good to put a note about supporting CORS in reference.md in REST API section