如何调试葡萄api

How to debug grape api

module Employee
    class Data < Grape::API
        resource :employee_data do 
            desc "Description of all employees"
            get do 
                EmpDatum.all
            end

            desc "create a new employee"
                params do 
                    requires :name, type: String
                    requires :address, type: String
                    requires :age, type: Integer
                end
                post  do
                    EmpDatum.create!({
               name: params[:name],
               address: params[:address],
               age: params[:age]

                        })
                end
            end
    end
end

获取请求工作正常。但是,当发送 POST 请求时,

curl http://localhost:3000/api/v1/employee_data.json -d "name='jay';address='delhi';age=25"

{"error":"address is missing, age is missing"}%      

我无法将 byebug 放入上面的 post 块中。我看不到正在传递的参数。我在这里错过了什么

您的 API 实施没有任何问题。但是,您错误地使用了 cURL。拨打这样的电话:

curl -i -X POST -H "Content-Type:application/json" http://localhost:3000/api/v1/employee_data.json -d '{"name":"jay","address":"Delhi","age":"25"}'

此外,我不确定 bybug 是什么,但我已经使用 RubyMine 一段时间了并且非常适合调试 Ruby Grape APIs。如果您使用 bybug 没有成功,您可以在 API 方法中看到您收到的内容,只需将您的方法代码替换为:

puts params.inspect

例如:

module Employee
    class Data < Grape::API
        resource :employee_data do 
            desc "Description of all employees"
            get do 
                EmpDatum.all
            end

            desc "create a new employee"
            params do 
                requires :name, type: String
                requires :address, type: String
                requires :age, type: Integer
            end
            post  do
                puts params.inspect
            end
        end
    end
end