我正在使用Swagger-UI浏览我自己的API,用葡萄构建并自动记录葡萄招摇.
我用Google搜索并尝试了我能找到的所有建议,但我无法让POST工作.这是我的标题:
header "Access-Control-Allow-Origin", "*"
header "Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, PATCH, DELETE"
header "Access-Control-Request-Method", "*"
header "Access-Control-Max-Age", "1728000"
header "Access-Control-Allow-Headers", "api_key, Content-Type"
Run Code Online (Sandbox Code Playgroud)
我只是扔了一切建议.我已经在supportedSubmitMethods中启用了所有HTTP方法,并且我使用POSTMAN Chrome扩展程序测试了API,它运行良好.正确创建用户并返回正确的数据.
然而,我得到的所有内容都是服务器报告:
Started OPTIONS "/v1/users.json" for 127.0.0.1 at 2012-12-21 04:07:13 -0800
Run Code Online (Sandbox Code Playgroud)
和swagger的反应看起来像这样:
请求网址
http://api.lvh.me:3000/v1/users.json
Run Code Online (Sandbox Code Playgroud)
响应机构
响应代码
0
Run Code Online (Sandbox Code Playgroud)
响应标题
我还用POSTMAN测试了OPTIONS响应,它位于:
Allow ?OPTIONS, GET, POST
Cache-Control ?no-cache
Date ?Fri, 21 Dec 2012 12:14:27 GMT
Server ?Apache-Coyote/1.1
X-Request-Id ?9215cba8da86824b97c6900fb6d97aec
X-Runtime ?0.170000
X-UA-Compatible ?IE=Edge
Run Code Online (Sandbox Code Playgroud) 我正在使用Grape on Rails 4.2构建API.这是GitHub上的回购链接.
在前端,我有一个用EmberJS构建的JavaScript应用程序.这里也是GitHub上回购的链接.
我已经更新了以下宝石,以便我可以按照JSON API标准格式化我的API的响应,这似乎需要更新到Ember 1.13,然后更新到Ember 2.0.
gem "active_model_serializers", '0.10.0.rc2'
gem "grape"
gem "grape-active_model_serializers", :git => 'https://github.com/jrhe/grape-active_model_serializers.git'
Run Code Online (Sandbox Code Playgroud)
之后,当我调用API时出现以下错误:IOError:未打开进行读取
根据我在这里和那里收集的信息,我怀疑这与我格式化JSON的方式存在冲突.
在default.rb中,当我对此行进行注释时,继承所有API控制器的文件:
formatter :json, Grape::Formatter::ActiveModelSerializers
Run Code Online (Sandbox Code Playgroud)
我不再有任何错误,但显然响应没有序列化.
我的问题是:
如果您需要更多信息,请与我们联系.
在此先感谢您的帮助.
我正在Grape gem的帮助下构建XML API.为API操作构建XML的最佳方法是什么?由于Grape没有使用标准轨道控制器,我无法使用views/../xml.builder.你有什么建议?也许有一些构建xml api的最佳实践?
我需要创建一个POST,我可以在同一个请求中上传多个文件,但我不知道如何用葡萄写这个.现在只上传一个文件,这就是我正在做的事情,并且工作正常:
desc 'Creates a new attachment.'
params do
requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Attachment File."
end
post do
attachment = Attachment.new
attachment.file = ActionDispatch::Http::UploadedFile.new(params[:file])
attachment.save!
attachment
end
Run Code Online (Sandbox Code Playgroud)
Swagger告诉我这个:

我在考虑做这样的事情:
desc 'Creates a new attachment.'
params do
requires :file, :type => Array[Rack::Multipart::UploadedFile], :desc => "Attachment File."
end
Run Code Online (Sandbox Code Playgroud)
但它看起来不太好看:

我也尝试过:
params do
optional :attachments, type: Array do
requires :file, :type => Rack::Multipart::UploadedFile, :desc => "Attachment File."
end
end
Run Code Online (Sandbox Code Playgroud)
也不是一个好结果.

处理这个问题的正确方法是什么?
我在理解Grape API方面遇到了很多麻烦,特别route_param是它如何与它一起工作params.
考虑以下代码:
desc "Return a status."
params do
requires :id, type: Integer, desc: "Status id."
end
route_param :id do
get do
Status.find(param[:id])
end
end
Run Code Online (Sandbox Code Playgroud)
这个街区产生什么路线?我知道这是一个get请求,但为什么它被包裹在route_param块中?为什么不能params阻止?
我正在使用active_model_serializer 0.10.0.rc5和grape gemapi.
我有一个这样的帖子端点:
class V1::Endpoints::Posts < Grape::API
resource :posts do
desc 'Returns a list of posts.'
# serializing array
get '', each_serializer: V1::Serializers::PostSerializer do
@posts = Post.all
present @posts
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的序列化程序看起来像这样:
class V1::Serializers::PostSerializer < ActiveModel::Serializer
attributes :id, :name, :slug
end
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试访问post端点时,我收到以下错误:
ActiveModel::Serializer::CollectionSerializer::NoSerializerError - No serializer found for resource:
Run Code Online (Sandbox Code Playgroud)
我在调试问题时想出的问题在于CollectionSerializer#initialize这个gem.我想这个serializer_class变量是零.
我已经尝试了几乎所有与此问题相关的链接.但没有一个对我有用.
当使用 Grape 编写 API 时,为什么要费心使用helpers宏,而不是仅仅包含一个模块或添加一个方法?
例如,您可以在模块中定义方法并将它们作为帮助程序包含在 Grape 中,如下所示:
module HelperMethods
def useful_method(param)
"Does a thing with #{param}"
end
end
class HelpersAPI < Grape::API
helpers HelperMethods
get 'do_stuff/:id' do
useful_method(params[:id])
end
end
Run Code Online (Sandbox Code Playgroud)
但是,为什么不这样做呢?
class IncludeAPI < Grape::API
include HelperMethods
get 'do_stuff/:id' do
useful_method(params[:id])
end
end
Run Code Online (Sandbox Code Playgroud)
我想您HelperMethods为了提供辅助方法而包含该模块更为明确,但这似乎是添加替代语法的一个微不足道的理由。
helpers与只是普通的相比,您想要使用的好处/原因是include什么?
我正在使用grape、grape-swagger、 和grape-swagger-rails为我的 Rails 4.2.1 项目设置 API。它将是一个内部 API,因此我希望开发人员能够访问它,但普通公众不能访问它。最好的方法是什么?
我最初的想法是利用api_key可以在 中设置的字段swagger-ui,但我无法找出在 Grape 的根 API 生成器中访问它的正确方法。
我还认为,如果用户是管理员(我们正在使用 Devise),我可以尝试仅安装文档的端点,但这不会隐藏文档本身(如果有人知道链接swagger.json)。
过去人们是如何处理这个问题的?
我如何在grape API中实现authenticate_or_request_with_http_token。下面是我的代码:
module Articles
class ArticleData < Grape::API
include ActionController::HttpAuthentication::Token::ControllerMethods
# http_basic do |email, password|
# user = User.find_by_email(email)
# user && user.valid_password?(password)
# end
before do
error!("401 Unauthorized", 401) unless authenticated
end
helpers do
def authenticated
authenticate_or_request_with_http_token do |token, options|
apiKey = ApiKey.where(auth_token: token).first
#ApiKey.exists?(access_token: token)
end
end
end
resource :article_data do
desc "Return all article data"
get do
Article.all
end
desc "create a new article"
## This takes care of parameter validation
params do
requires :title, type: String …Run Code Online (Sandbox Code Playgroud) 我正在我的Rails项目中安装Grape来构建RESTful API.
现在一些端点有需要身份验证的操作和其他不需要身份验证的端点.
例如,我有users一个看起来像这样的终点:
module Backend
module V1
class Users < Grape::API
include Backend::V1::Defaults
before { authenticate! }
resource :users do
desc "Return a user"
params do
requires :id, type: Integer, desc: 'User id'
end
get ':id' do
UsersService::Fetch.new(current_user,params).call
end
desc "Update a user"
params do
requires :id, type: Integer, desc: 'User id'
requires :display_name, type: String, desc: 'Display name'
requires :email, type: String, desc: 'Email'
end
post ':id' do
UsersService::Save.new(current_user,params).call
end
desc "Reset user password"
params do …Run Code Online (Sandbox Code Playgroud)