grape api ignores PUT/POST params

微笑、不失礼 提交于 2019-12-25 00:58:25

问题


I am building a grape api for a rails app. I am testing it with rspec request specs. I have encountered the problem when making a post route like this:

resources :events do
  segment '/:event_id' do
    resources :tickets do
       post do
         event = current_user.events.find params[:event_id]
         ...#do sth with event using params[:tickets_ids]

the corresponding spec:

it "should should return the JSON hash of validated tickets" do
  post "/api/mobile/#{version}/events/#{event.id}/tickets/", { tickets_ids: [1,2] }, credentials
  response.should be_success
  JSON.parse(response.body).should be_kind_of Hash
end

Grape 'controller' is not able to see any params which are not sent as a part of url. When printing out params[:tickets_ids] it is nil. I do not have this problem when using get requests. I am able to reproduce the issue when sending request manually using curl. Nested resources routing is also not a problem since it behaves the same with top level route.

I have found the following issue on github but applying the given monkey patch does not work and breaks the server on startup. https://github.com/intridea/grape/issues/417

I am using the following version of gems and am unable to upgrade them due to dependency conflicts:

grape (0.2.1.1)
rails(3.0.7)
rspec-rails (2.6.1)

Any help and tips will be greatly appreciated.


回答1:


If I recall there should be a header set Content-Type with the value application/x-www-form-urlencoded



来源:https://stackoverflow.com/questions/18058115/grape-api-ignores-put-post-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!