gpt4 book ai didi

ruby-on-rails - Rails Grape API 'id is invalid' 在不需要 id 的请求中

转载 作者:行者123 更新时间:2023-12-04 03:41:33 24 4
gpt4 key购买 nike

我有一个由 Doorkeeper 保护的 Grape API,我有一堆完美的方法。但是,有一种方法表现得很奇怪。
这是一个不需要参数的 GET 请求,运行它会引发以下错误:

Grape::Exceptions::ValidationErrors at /v1/discount_cards/all.json

id is invalid



我的方法是这样的:
desc 'Get all the cards of current customer'
params {}
get 'all' do
current_customer.discount_cards.to_json(include:
{
barcode: {
include: :barcode_type
}
})
end

日志说错误发生在 logger.rb 的第 17 行文件,看起来像这样:
module API
class Logger
def initialize(app)
@app = app
end

def call(env)
payload = {
remote_addr: env['REMOTE_ADDR'],
request_method: env['REQUEST_METHOD'],
request_path: env['PATH_INFO'],
request_query: env['QUERY_STRING'],
x_organization: env['HTTP_X_ORGANIZATION']
}

ActiveSupport::Notifications.instrument 'grape.request', payload do
@app.call(env).tap do |response| # <-- logs say the error is here
payload[:params] = env['api.endpoint'].params.to_hash
payload[:params].delete('route_info')
payload[:params].delete('format')
payload[:response_status] = response[0]
end
end
end
end
end

我的主 base.rb类看起来像这样:
module API
class Dispatch < Grape::API
mount API::V1::Base
end

Base = Rack::Builder.new do
use API::Logger
run API::Dispatch
end
end

我真的不明白什么 id它正在谈论,而且,所有其他 api 方法都可以正常工作(发布、获取、放置、删除)。

你能帮我解决这个问题吗?

最佳答案

我最近也有这个问题,结果证明是我的代码顺序问题。我找到了答案 here .

Grape evaluates routes in order, so with two routes, /:id and /nearby_missions, it matches /:id first, making id=nearby_missions. Then it tries to coerce nearby_missions into an Integer which fails, and you get the missing id error.

You could "fix" this by adding requirements: /\d*/ to the first route (didn't test), but you're probably just better off ordering them in the order you want them to be evaluated, which is what you did.


您的问题中没有足够的信息让我确定这也是您的问题,但很可能是!

关于ruby-on-rails - Rails Grape API 'id is invalid' 在不需要 id 的请求中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34497615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com