【问题标题】:a get request doesn't return values获取请求不返回值
【发布时间】:2013-01-30 08:47:32
【问题描述】:

我正在尝试使用这篇文章:https://github.com/Licensario/licensario-rest-protocol/blob/master/rest-api.md#feature-usage

所以我写了:

## get request
feature_allocation_req = Net::HTTP::Get.new("/api/v1/users/external/#{user_id}/features/VIEW_TASKSed8e919e60/alloc?paymentPlanId=SUBSCRIPTI33dbf5e82a");

## add the headers
feature_allocation_req.add_field('ISV_API_KEY', 'bcffa3335a9068ab37ad51a68b4b47ea72f32a49c8f0b4f299a629edad04cdc3'); 
feature_allocation_req.add_field('ISV_API_SECRET', '14c990f3d4ee6354ee86fffdb057b3584f885c0d79cd3b354123e1b9acdc2227');

## start the get request
feature_allocation_result = nil
Net::HTTP.start("localhost", 3000) {|http|
    feature_allocation_result = http.request(feature_allocation_req)
}

## print the number of the response (I got 200 - OK)
puts feature_allocation_result.code

## deserialize JSON objects
feature_allocation_parameters = Yajl::Parser.parse(feature_allocation_result.body);
total_parameter = feature_allocation_parameters["total"];
used_parameter = feature_allocation_parameters["used"];
available_parameter = feature_allocation_parameters["available"];

## print them
puts "***********************"
puts total_parameter, used_parameter, available_parameter

但它什么也没打印。我写错了什么吗? (我认为如果我有问题,问题是在“反序列化JSON对象”的步骤中发现的,但我尝试了:JSON.parse,它也不起作用)..

任何帮助表示赞赏。

【问题讨论】:

  • feature_allocation_result.body 中有什么内容?
  • 每个值为空:{"total":null,"used":null,"available":null}
  • 是的,所以问题在另一端。它向您发送 nil 值。
  • 好的,所以你可以添加你的答案,我会标记它:] 谢谢!

标签: ruby-on-rails json rest protocols


【解决方案1】:

我会尝试的第一件事是打印feature_allocation_result.body 的值。这将允许我确定对方是否发送了正确的 JSON 并且错误是我的。有时我得到一个 HTML 错误页面而不是 JSON,因为我的请求是错误的。打印body 有助于澄清这一点。

现在您已经完成了,我们看到该服务向您发送了有效的 JSON,但所有值都是 nil。

{"total":null,"used":null,"available":null}

我们的解析是好的(但如果它更复杂的话,它也可能不是)。无论如何,现在我们必须找出服务没有向我们发送我们期望的数据的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多