如何在EVE中使用CORS调用get on schema端点?

时间:2015-12-21 16:46:53

标签: python eve

在我的EVE配置中,我定义了SCHEMA_ENDPOINT='schema',这样当我执行get api_root/schema/resource时,我会获得该资源的架构。我也有X_DOMAINS='*',所以当我调用get api_root/resource/item时,我会从任何域调用我的API时获取我要求的项目。这些都现在分开工作。

但是,如果我调用api_root/schema/resource或者只调用api_root/schema,这将为我提供所有资源的架构,使用CORS发送OPTIONS预检请求,它会失败关于飞行前请求。因此,当我在不同的域上进行ajax调用而不是托管API的域时,我会在chrome中收到此错误消息:

XMLHttpRequest cannot load http://api.dev:5000/v1/schema/resource. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://test.dev:5002' is therefore not allowed access.

现在我对使用CORS的任何其他调用都没有这个问题。就像我说的那样,我在调用api_root/resource时没有这个问题。所以我不认为这是客户端问题。是否有某种服务器端实现可以让模式端点与CORS一起使用?

更新

问题在于发送OPTIONS而不是GET。例如:

$ curl -I -X OPTIONS  -H "Origin: test.com" 'http://127.0.0.1:5000/v1/schema/order' --verbose
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> OPTIONS /v1/schema/order HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:5000
> Accept: */*
> Origin: http://test.com
> 
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Allow: HEAD, OPTIONS, GET
< Content-Length: 0
< Server: Eve/0.6.2.dev0 Werkzeug/0.10.4 Python/2.7.6
< Date: Mon, 11 Jan 2016 16:57:39 GMT

1 个答案:

答案 0 :(得分:0)

我用CORS测试SCHEMA_ENDPOINT,一切似乎都按预期工作。在我的设置中,我有:

X_DOMAINS = '*'
SCHEMA_ENDPOINT = 'schema'

我的飞行前请求:

curl -H "Origin: http://test.com" --verbose http://localhost:5000/schema

> GET /schema/contacts HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.43.0
> Accept: */*
> Origin: http://test.com

< HTTP/1.0 200 OK
< Access-Control-Allow-Origin: http://test.com
< Vary: Origin
< Access-Control-Allow-Headers:
< Access-Control-Expose-Headers:
< Access-Control-Allow-Methods: HEAD, OPTIONS, GET
< Access-Control-Allow-Max-Age: 21600
< Server: Eve/0.6.2.dev0 Werkzeug/0.10.4 Python/2.7.8

/schema/resource也是如此。请注意,-Headers为空,因为在我的测试中,我没有设置任何X_HEADERSX_EXPOSE_HEADERS

编辑:更新后,我发现问题是({1}}。它已在上游修复,将与v0.6.2一起发布。