【问题标题】:Posting JSON in Ruby with HTTParty Returns invalid_request使用 HTTParty 在 Ruby 中发布 JSON 返回 invalid_request
【发布时间】:2015-10-18 18:18:54
【问题描述】:

我的帖子似乎到达了主机,但是它的格式可能不正确?不以 JSON 格式输入?

主持人的结果是给我一个错误代码:invalid_request 消息:请求验证失败。

主机文档:

这意味着自动订购没有足够的信息来 下订单。这几乎总是发生,因为你没有完全 在您的自动订购中填写您的亚马逊账户信息 部分。

我的错误结果包括以下内容:

{
  "_type": "error",
  "_request_id": "5623dcc59ca1bb1e76000a9f",
  "code": "invalid_request",
  "message": "Validation failed on the request.",
  "data": {
    "validator_errors": [
      {
        "path": "retailer_credentials.password",
        "message": "'undefined' is not a permitted value for 'retailer_credentials.password' field."
      },
      {
        "path": "retailer_credentials.email",
        "message": "'undefined' is not a permitted value for 'retailer_credentials.email' field."
      },
      {
        "path": "shipping_method",
        "message": "'undefined' is not a permitted value for 'shipping_method' field."
      },
      {
        "path": "is_gift",
        "message": "'undefined' is not a permitted value for 'is_gift' field."
      },
      {
        "path": "shipping_address.phone_number",
        "message": "'undefined' is not a permitted value for 'shipping_address.phone_number' field."
      },
      {
        "path": "shipping_address.country",
        "message": "'undefined' is not a permitted value for 'shipping_address.country' field."
      },
      {
        "path": "shipping_address.city",
        "message": "'undefined' is not a permitted value for 'shipping_address.city' field."
      },
      {
        "path": "shipping_address.zip_code",
        "message": "'undefined' is not a permitted value for 'shipping_address.zip_code' field."
      },
      {
        "path": "shipping_address.address_line1",
        "message": "'undefined' is not a permitted value for 'shipping_address.address_line1' field."
      },
      {
        "path": "shipping_address.last_name",
        "message": "'undefined' is not a permitted value for 'shipping_address.last_name' field."
      },
      {
        "path": "shipping_address.first_name",
        "message": "'undefined' is not a permitted value for 'shipping_address.first_name' field."
      },
      {
        "path": "retailer",
        "message": "'undefined' is not a permitted value for 'retailer' field."
      },
      {
        "path": "client_token",
        "message": "'undefined' is not a permitted value for 'client_token' field."
      },
      {
        "value": [],
        "path": "products",
        "message": "'' is not a permitted value for 'products' field."
      }
    ]
  },
  "host": "zincapi-1",
  "offers_urls": [],
  "screenshot_urls": [],
  "_created_at": "2015-10-18T17:54:13.416Z"
}

我的代码在索引视图中为:

<table>
  <thead>
    <tr>
      <th>IF name</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
<% @ifs.each do |t| %>

<!-- Update Each cur_count -->

  <%
  require 'json'
  require 'HTTParty'


  zincUrl = 'https://api.zinc.io/v0/order'
  @ZincResult=HTTParty.post(zincUrl,
    :body => [{
      :client_token => "XXXXXXXX",
  :retailer => "amazon",
  :products => [{"product_id" => ""+t.item+"", "quantity" => 1,
  "seller_selection_criteria" => [
      {
        "condition_in" => ["New"],
        "international" => false,
        "max_shipping_days" => 10
      }
    ]
    }],
  :max_price => 2000,
  :shipping_address => {
  "first_name" => "name",
  "last_name" => "name",
  "address_line1" => "address",
  "address_line2" => "",
  "zip_code" => "94133",
  "city" => "San Francisco",
  "state" => "CA",
  "country" => "US",
  "phone_number" => "number"
  },
  :is_gift => true,
  :gift_message => "present",
  :shipping_method => "cheapest",
  :payment_method => {
  "name_on_card" => "name",
  "number" => "XXXXXXXX",
  "security_code" => "XXX",
  "expiration_month" => "XX",
  "expiration_year" => "XXXX",
  "use_gift" => false
  },
  :billing_address => {
  "first_name" => "name",
  "last_name" => "name",
  "address_line1" => "address",
  "address_line2" => "",
  "zip_code" => "94133",
  "city" => "San Francisco",
  "state" => "CA",
  "country" => "US",
  "phone_number" => "XXXXXXXX"
  },
  :retailer_credentials => {
  "email" => "email",
  "password" => "password"
  },
  :webhooks => {
  "order_placed" => "http://mywebsite.com/zinc/order_placed",
  "order_failed" => "http://mywebsite.com/zinc/order_failed",
  "tracking_obtained" => "http://mywebsite.com/zinc/tracking_obtained"
  },
  :client_notes => {
  "our_internal_order_id" => "Order_"+t.item+"",
  }
  }].to_json,

    :headers => { 'Content-Type' => 'application/json' } )

  %>

  <%puts "end of Zinc if loop"%>

  <%puts @ZincResult%>

<tr>
  <td><%= t.name %>  </td>
  <td><%= t.item %></td>
  <td><%= t.key %></td>
  <td><%= t.count %></td>
  <td><%= t.cur_count %></td>

  <td><%= link_to 'show', if_path(t) %></td>

  <td><%= link_to 'edit', edit_if_path(t.id) %></td>

  <td><%= link_to 'delete', ifs_delete_path(:id =>t.id)%></td>

</tr>
<% end %>
</tbody>
</table>

【问题讨论】:

  • 你为什么要在你的视图中进行 API 调用?那是你应该进行 API 调用的最后一个地方。
  • 你想在这里做什么?
  • @ThomasRoss 和 Max 我正在尝试使用上述主体调用“api.zinc.io/v0/order”API。我可以在 Python 中做到这一点,但现在正试图让它在 Ruby 上运行。我现在把它作为一种方法放在控制器中。但是,我在调用该方法时仍然收到上述错误。非常感谢您看一看,我一直在努力解决这个问题。

标签: ruby-on-rails ruby json httparty


【解决方案1】:

根据Zinc Docs,您应该在正文中发送 json 对象,但您正在发送 json 数组。要发送 JSON 对象,您应该以花括号“{}”而不是方括号“[]”开始您的正文

@ZincResult=HTTParty.post(zincUrl,
    :body => {.........}.to_json,
    :headers => { 'Content-Type' => 'application/json' } )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 2019-06-11
    相关资源
    最近更新 更多