Open juhoinkinen opened 2 weeks ago
A question is whether we want the language detection functionality of Annif instances to be directly usable by other websites (the current situation does not restrict the use of via other means than direct browser requests).
While implementing the language detection feature for FintoAI (https://github.com/NatLibFi/FintoAI/issues/9, https://github.com/NatLibFi/FintoAI/pull/21), I encountered an issue with the
detect-language
endpoint of Annif. When making a request to it, a local Annif instance logs the following error:This seems to be because the POST request is not a "simple request" due to the
Content-Type
beingapplication/json
. In contrast, thesuggest
endpoint usesapplication/x-www-form-urlencoded
, making it a simple request (see documentation).Steps to Reproduce
detect-language
endpoint withContent-Type: application/json
from Javascript code running in a browser (not from the Annif instance itself).Expected Behavior
The server should handle the OPTIONS preflight request correctly and allow the POST request to proceed.
Additional Context
I consulted Claude.ai and received the following insights:
Example OpenAPI v3 spec for OPTIONS method:
By defining the OPTIONS method and setting the appropriate CORS headers, the server should handle the preflight request correctly, allowing the POST request to proceed without errors.
Example of OPTIONS method OpenAPI 3.0 definition in AWS documentation here.
Note that when Annif is deployed and served from a web domain, the request to Annif comes from the same origin, and CORS is not needed anyway; this issue concerns only using the language detection feature from other websites (and local development, but the CORS protection can be disabled e.g. with Chromium by starting it with
chromium --disable-web-security --user-data-dir=~/tmp
.Resolution options
We update the Annif API to include the OPTIONS method for the
detect-language
endpoint and ensure it responds with the necessary CORS headers.Alternatively we could make the
detect-language
method accept data typeapplication/x-www-form-urlencoded
.