请求中不需要ID的Rails Grape API"ID无效" [英] Rails Grape API 'id is invalid' in request that requires no id

查看:87
本文介绍了请求中不需要ID的Rails Grape API"ID无效"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由Doorkeeper保护的Grape API,并且我有很多方法都可以正常工作.但是,有一种方法的行为很奇怪.这是一个不需要任何参数的GET请求,运行它会引发以下错误:

I have a Grape API protected by Doorkeeper and I have a bunch of methods which work perfectly. However, there's one method that behaves weirdly. It is a GET request that requires no parameters and running it throws the following error:

/v1/discount_cards/all.json中的Grape :: Exceptions :: ValidationErrors

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

id无效

我的方法如下:

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行,如下所示:

The logs say that the error happens at line 17 of the logger.rb file, which looks like this:

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方法都可以正常工作(发布,获取,放置,删除).


I really cannot understand what id it is talking about, and, moreover, all the other api methods work absolutely fine (post, get, put, delete).

您能帮助我解决该问题吗?

Could you help me with resolving that issue?

推荐答案

我最近也遇到了这个问题,事实证明这对我来说是一个代码顺序问题.我在此处找到了答案.

I had this issue recently too, and it turned out to be an issue of code order for me. I found the answer here.

Grape按顺序评估路由,因此使用/:id和/nearby_missions这两个路由,它首先匹配/:id,使id = nearby_missions.然后,它会尝试将Nearest_missions强制转换为Integer,该整数将失败,并且您会收到缺少的ID错误.

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.

您可以修复"通过在第一个路径中添加要求:/\ d */(没有测试),但是最好按您希望它们被评估的顺序订购它们,这就是您所做的.

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.

对于我来说,您的问题中没有足够的信息,以确保这也是您的问题,但是很可能是这样!

There's not enough info in your question for me to be sure that this is also your problem, but it could well be!

这篇关于请求中不需要ID的Rails Grape API"ID无效"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆