【问题标题】:How to decode encoded query params in GET request using grape-api如何使用grape-api解码GET请求中的编码查询参数
【发布时间】:2018-08-07 13:34:45
【问题描述】:

我正在使用 grape-api,但在使用编码查询参数从我的前端服务发出 GET 请求时遇到问题.这就是我的端点设置为接收参数的方式:

params do
  requires :event_id, type: String, desc: 'Event id'
  requires :tickets, type: Array, desc: 'Array of each ticket data ex:  [ {id: "", count: 1 , coupon_code: ""} ]', allow_blank: false do
    requires :id, type: String, desc: 'Ticket id'
    requires :count, type: Integer, desc: 'Number of tickets to get'
    optional :coupon_code, type: String, desc: 'Promocode to apply if any'
  end
end

如您所见,我期待一个 event_id 参数和另一个对象数组参数 tickets,这就是请求 URL 的样子:

http://localhost:3000/api/service_fees/calculate?eventId=2xy6rft69azlu2mtppnzb1xe6olzd3f0&tickets[]=%7B%22id%22:%22vohd3y3n25cdgbvi3uzmqhcyie3zi53a%22,%22count%22:2%7D

但是,grape 似乎不理解这些编码的查询参数,我得到了这个异常:

[Exception: event_id is missing, tickets[0][id] is missing, tickets[0][id] is invalid, tickets[0][count] is missing, tickets[0][count] is invalid]

我的问题是:有没有办法告诉葡萄正确解码和解析查询参数?我在这里错过了什么吗?

【问题讨论】:

  • 可以显示服务器日志吗?
  • 我在您显示的 URL 中看不到任何 event_id 参数。它是eventId

标签: ruby-on-rails ruby rack grape grape-api


【解决方案1】:
http://localhost:3000/api/service_fees/calculate?eventId=2xy6rft69azlu2mtppnzb1xe6olzd3f0&tickets[]=%7B%22id%22:%22vohd3y3n25cdgbvi3uzmqhcyie3zi53a%22,%22count%22:2%7D

对于上述请求 URL,发送的参数名称是 eventID,而 Grape API 需要/期望 event_id。另外,tickets URL Query String Parameters 构造错误。

你能不能试试这个——http://localhost:3000/api/service_fees/calculate?eventId=2xy6rft69azlu2mtppnzb1xe6olzd3f0&tickets[0][id] =12&tickets[0][count] =13&tickets[0][coupon_code]=amrrbakry

【讨论】:

    猜你喜欢
    • 2016-11-06
    • 1970-01-01
    • 2013-09-08
    • 2013-05-08
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    相关资源
    最近更新 更多