我正在尝试使用Rails 4.0.4和Grape0.7.0为REST创建一个框架,其行为如下:
使用特定版本调用API:
$ curl -H Accept=application/vnd.acme-v1+json http://localhost:3000/api/a
“a-v1”
$ curl -H Accept=application/vnd.acme-v1+json http://localhost:3000/api/b
“b-v1”以默认方式调用API:
$ curl http://localhost:3000/api/a
“a-v2”
$ curl http://localhost:3000/api/b
“b-v1”
$ curl http://localhost:3000/api/c
“c-v2”我一直在努力,但我没有得到想要的行为。最后,我在rails应用程序中获得了以下文件:
app/api/api.rb
require 'grape'
require 'api_v1.rb'
require 'api_v2.rb'
module API
class Base < Grape::API
mount API::V2
mount API::V1
end
endapp/api/api_v1.rb
require 'grape'
module API
class V1 < Grape::API
version 'v1', using: :header, vendor: 'acme', format: :json
prefix 'api'
format :json
get :a do
"a-v1"
end
get :b do
"b-v1"
end
end
endapp/api/api_v2.rb
require 'grape'
module API
class V2 < Grape::API
version ['v2', 'v1'], using: :header, vendor: 'acme', cascade: true
prefix 'api'
format :json
get :a do
"a-v2"
end
get :c do
"c-v2"
end
end
endapp/config/scripes.rb
...
mount API::Base => '/'
...对于上面的文件,不管我在curl命令中指定了哪个版本,我都会得到默认的行为。
发布于 2014-04-20 04:56:33
葡萄(据我所知)不允许API类指定版本字符串数组,但我认为您不需要在这里这样做。此外,您的卷发语法是不正确的。
一旦我将version行在app/api/api_v2.rb中更改为
version 'v2', using: :header, vendor: 'acme', cascade: true并使用正确的-H参数语法调用curl (请注意冒号而不是等号):
$ curl -H Accept:application/vnd.acme-v1+json http://localhost:3000/api/a
"a-v1"
$ curl -H Accept:application/vnd.acme-v1+json http://localhost:3000/api/b
"b-v1"
$ curl http://localhost:3000/api/a
"a-v2"
$ curl http://localhost:3000/api/b
"b-v1"
$ curl http://localhost:3000/api/c
"c-v2"https://stackoverflow.com/questions/23178034
复制相似问题