Easily add Cross-origin resource sharing (CORS) to a Service, a Route by enabling this plugin.
Terminology
plugin
: a plugin executing actions inside IAM before or after a request has been proxied to the upstream API.Service
: the IAM entity representing an external upstream API or microservice.Route
: the IAM entity representing a way to map downstream requests to upstream services.upstream service
: this refers to your own API/service sitting behind IAM, to which client requests are forwarded.
Configuration
This plugin is compatible with requests with the following protocols:
http
https
This plugin is compatible with DB-less mode.
Enabling the plugin on a Service
With a database
Configure this plugin on a Service by making the following request:
$ curl -X POST http://localhost:8001/services/{service}/plugins \
--data "name=cors" \
--data "config.origins=http://mockbin.com" \
--data "config.methods=GET" \
--data "config.methods=POST" \
--data "config.headers=Accept" \
--data "config.headers=Accept-Version" \
--data "config.headers=Content-Length" \
--data "config.headers=Content-MD5" \
--data "config.headers=Content-Type" \
--data "config.headers=Date" \
--data "config.headers=X-Auth-Token" \
--data "config.exposed_headers=X-Auth-Token" \
--data "config.credentials=true" \
--data "config.max_age=3600"
Without a database
Configure this plugin on a Service by adding this section do your declarative configuration file:
plugins:
- name: cors
service: {service}
config:
origins: http://mockbin.com
methods:
- GET
- POST
headers:
- Accept
- Accept-Version
- Content-Length
- Content-MD5
- Content-Type
- Date
- X-Auth-Token
exposed_headers: X-Auth-Token
credentials: true
max_age: 3600
In both cases, {service}
is the id
or name
of the Service that this plugin configuration will target.
Enabling the plugin on a Route
With a database
Configure this plugin on a Route with:
$ curl -X POST http://localhost:8001/routes/{route}/plugins \
--data "name=cors" \
--data "config.origins=http://mockbin.com" \
--data "config.methods=GET" \
--data "config.methods=POST" \
--data "config.headers=Accept" \
--data "config.headers=Accept-Version" \
--data "config.headers=Content-Length" \
--data "config.headers=Content-MD5" \
--data "config.headers=Content-Type" \
--data "config.headers=Date" \
--data "config.headers=X-Auth-Token" \
--data "config.exposed_headers=X-Auth-Token" \
--data "config.credentials=true" \
--data "config.max_age=3600"
Without a database
Configure this plugin on a Route by adding this section do your declarative configuration file:
plugins:
- name: cors
route: {route}
config:
origins: http://mockbin.com
methods:
- GET
- POST
headers:
- Accept
- Accept-Version
- Content-Length
- Content-MD5
- Content-Type
- Date
- X-Auth-Token
exposed_headers: X-Auth-Token
credentials: true
max_age: 3600
In both cases, {route}
is the id
or name
of the Route that this plugin configuration will target.
Global plugins
- Using a database, all plugins can be configured using the
http://localhost:8001/plugins/
endpoint. - Without a database, all plugins can be configured via the
plugins:
entry on the declarative configuration file.
A plugin which is not associated to any Service, Route or Consumer (or API, if you are using an older version of IAM) is considered "global", and will be run on every request. Read the Plugin Reference and the Plugin Precedence sections for more information.
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
form parameter | description |
---|---|
name | The name of the plugin to use, in this case cors |
service_id | The id of the Service which this plugin will target. |
route_id | The id of the Route which this plugin will target. |
enabled default value: true | Whether this plugin will be applied. |
config.origins
optional |
List of allowed domains for the |
config.methods
optional default value:
|
Value for the |
config.headers
optional default value: Value of the |
Value for the |
config.exposed_headers
optional |
Value for the |
config.credentials
optional default value:
|
Flag to determine whether the |
config.max_age
optional |
Indicated how long the results of the preflight request can be cached, in |
config.preflight_continue
optional default value:
|
A boolean value that instructs the plugin to proxy the |
Known issues
Below is a list of known issues or limitations for this plugin.
CORS Limitations
If the client is a browser, there is a known issue with this plugin caused by a
limitation of the CORS specification that doesn't allow to specify a custom
Host
header in a preflight OPTIONS
request.
Because of this limitation, this plugin will only work for Routes that have been
configured with a paths
setting, and it will not work for Routes that
are being resolved using a custom DNS (the hosts
property).
To learn how to configure paths
for a Route, please read the Proxy
Reference.