【问题标题】:API POST with array using HTTP gem (or RestClient)使用 HTTP gem(或 RestClient)的数组 API POST
【发布时间】:2018-03-09 06:07:57
【问题描述】:

我在使用这个 api 时遇到了问题,似乎无法克服困难。使用HTTP gem(虽然我很灵活,可以使用RestClient,如果这能让我更快地得到答案!)。无论如何,我在发布数组时遇到了麻烦。其他一切都很好,我只是无法在 addorder 方法中找到的 printaura api 中找出这个“itemsarray”:PrintAura API

我正在运行这个:

def self.submitorder   
  req = HTTP.post("https://api.printaura.com/api.php", :json => { 
        :key => APIKEY, 
        :hash => APIHASH, 
        :method => "addorder",
        :businessname => "this is a secret too",
        :businesscontact => "thats a secret",
        :email => "my@email.com",
        :your_order_id => "1",
        :returnlabel => "FakeAddress",
        :clientname => "ShippingName",
        :address1 => "ShippingAddressLine1",
        :address2 => "ShippingAddressLine2",
        :city => "ShippingCity",
        :state => "ShippingState",
        :zip => "ShippingZip",
        :country => "US",
        :customerphone => "dontcallme",
        :shipping_id => "1",
        :itemsarray => {:item => [ 
            :product_id => 423,
            :brand_id => 33,
            :color_id => 498,
            :size_id => 4,
            :front_print => 1389517,
            :front_mockup => 1390615,
            :quantity => 1
          ]}

    })

  puts JSON.parse(req)




end

我的输出是这样的:

{"status"=>false, "error_code"=>19, "result"=>19, "message"=>"You cannot place an order without items, Please fill the items array with all the required information. Full API documentation can be found at https:/www.printaura.com/api/"}

天哪,如果有人能看到并帮助我,我将永远感激它。

【问题讨论】:

    标签: ruby-on-rails ruby api rest-client http.rb


    【解决方案1】:
    def self.submitorder 
      itemsarray = { :items => [ { :product_id => 423, :brand_id => 33, :color_id => 498, :size_id => 4, :quantity => 1, :front_print => 1389517,
            :front_mockup => 1390617 } ] }
      req = HTTP.post("https://api.printaura.com/api.php", :json => { 
        :key => APIKEY, 
        :hash => APIHASH, 
        :method => "addorder",
        :businessname => "this is a secret too",
        :businesscontact => "thats a secret",
        :email => "my@email.com",
        :your_order_id => "1",
        :returnlabel => "FakeAddress",
        :clientname => "ShippingName",
        :address1 => "ShippingAddressLine1",
        :address2 => "ShippingAddressLine2",
        :city => "ShippingCity",
        :state => "ShippingState",
        :zip => "ShippingZip",
        :country => "US",
        :customerphone => "dontcallme",
        :shipping_id => "1",
        :items =>  Base64.encode64(itemsarray.to_json)}
    )
    
      puts JSON.parse(req)
    

    我真的希望这能在几年后对某人有所帮助,哈哈

    【讨论】:

      【解决方案2】:

      要在 JSON 中创建一个数组,您可以在 Ruby 中使用一个数组。就这么简单。

      require 'json'
      
      def self.submitorder   
        req = HTTP.post("https://api.printaura.com/api.php", :json => { 
              :key => APIKEY, 
              :hash => APIHASH, 
              :method => "addorder",
              :businessname => "this is a secret too",
              :businesscontact => "thats a secret",
              :email => "my@email.com",
              :your_order_id => "1",
              :returnlabel => "FakeAddress",
              :clientname => "ShippingName",
              :address1 => "ShippingAddressLine1",
              :address2 => "ShippingAddressLine2",
              :city => "ShippingCity",
              :state => "ShippingState",
              :zip => "ShippingZip",
              :country => "US",
              :customerphone => "dontcallme",
              :shipping_id => "1",
              :items => [ 
                 {
                   :product_id => 423,
                   :brand_id => 33,
                   :color_id => 498,
                   :size_id => 4,
                   :front_print => 1389517,
                   :front_mockup => 1390615,
                   :quantity => 1
                 }
              ]
          })
      
        puts JSON.parse(req)
      

      API 列出了一个items 参数,该参数应该包含一个对象数组。它没有提到itemsarray

      【讨论】:

      • 那没用,同样的信息。我很困惑。我之前也尝试过。我只是想,因为在他们的文档中的 php 示例中它从 $postfield 更改为 $itemsarray 我需要制作一个“itemsarray”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 2019-12-31
      • 2014-09-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2013-07-08
      相关资源
      最近更新 更多