使用HTTP gem(或RestClient)的数组的API POST

时间:2017-09-27 16:46:00

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

我遇到麻烦这个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/"}

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

2 个答案:

答案 0 :(得分:2)

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)

我真的希望这可以帮助某人从现在开始几年哈哈

答案 1 :(得分:1)

要在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