葡萄 api 忽略 PUT/POST 参数

编程入门 行业动态 更新时间:2024-10-27 16:33:07
本文介绍了葡萄 api 忽略 PUT/POST 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在为一个 rails 应用程序构建一个葡萄 api.我正在使用 rspec 请求规范对其进行测试.我在做这样的post路由时遇到了这个问题:

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]

对应的规格:

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' 无法看到任何不作为 url 一部分发送的参数.打印 params[:tickets_ids] 时,它为零.使用 get 请求时我没有这个问题.使用 curl 手动发送请求时,我能够重现该问题.嵌套资源路由也不是问题,因为它的行为与顶级路由相同.

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.

我在 github 上发现了以下问题,但是应用给定的猴子补丁不起作用并且在启动时破坏了服务器.https://github/intridea/grape/issues/417

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/intridea/grape/issues/417

我正在使用以下版本的 gems,由于依赖冲突而无法升级它们:

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.

推荐答案

如果我记得应该有一个标题集 Content-Type,其值为 application/x-www-form-urlencoded

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

这篇关于葡萄 api 忽略 PUT/POST 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 04:21:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/930578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:葡萄   参数   PUT   api   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!